Module io.inverno.mod.web.server
Annotation Interface SseEventFactory
Binds a server-sent events factory
to a web route method parameter.
The type of the annotated parameter can either:
- be a
ResponseBody.Sse.EventFactory<ByteBuf, ResponseBody.Sse.Event<ByteBuf>>
to produce server-sent event with raw payload - considering type
T
which is not aByteBuf
, be aResponseBody.Sse.EventFactory<T, ResponseBody.Sse.Event<T>>
to produce server-sent event with encoded data
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
-
Element Details
-
value
String valueReturns 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"
-