Annotation Interface SseEventFactory


@Documented @Retention(SOURCE) @Target(PARAMETER) public @interface SseEventFactory

Binds a server-sent events factory to a web route method parameter.

The type of the annotated parameter can either:

The server-sent events factory is used in the handler to create server-sent events that are usually emitted in a Mono, Flux or Publisher returned by the route handler.

The event data is encoded using one of the MediaTypeConverter injected in the web server module and corresponding to the media type specified in the annotation.


 @WebRoute(path = "/events/raw", method = Method.GET)
 public Publisher<ResponseBody.Sse.Event<ByteBuf>> get_raw_events(@SseEventFactory ResponseBody.Sse.EventFactory<ByteBuf, ResponseBody.Sse.Event<ByteBuf>> events) {
     return Flux.interval(Duration.ofSeconds(1))
         .map(seq -> events.create(
             event -> event
                 .id(Long.toString(seq))
                 .event("event_raw")
                 .value(Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Event " + seq, Charsets.DEFAULT)))
             )
         );
 }

 @WebRoute(path = "/events/encoded", method = Method.GET)
 public Publisher<ResponseBody.Sse.Event<ByteBuf>> get_encoded_events(@SseEventFactory(MediaTypes.APPLICATION_JSON) ResponseBody.Sse.EventFactory<Book, ResponseBody.Sse.Event<Book>> events) {
     return Flux.interval(Duration.ofSeconds(1))
         .map(seq -> events.create(
             event -> event
                 .id(Long.toString(seq))
                 .event("event_encoded")
                 .value(new Book(...))
             )
         );
 }
 
Since:
1.0
Author:
Jeremy Kuhn
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Returns the media type of the server-sent event data.
  • Element Details

    • value

      String value

      Returns the media type of the server-sent event data.

      This media type is used to determine the MediaTypeConverter to use to encode the data of the event.

      Returns:
      a media type
      Default:
      "text/plain"