public interface IReactiveSSEDataDriverContextVariable extends IReactiveDataDriverContextVariable
Interface to be implemented by context variables wrapping asynchronous objects in the form of reactive data streams which are meant to drive the reactive-friendly execution of a template in SSE (Server-Sent Event) mode.
This interface adds to its parent IReactiveDataDriverContextVariable
the possibility to specify a prefix
to be applied to the names and IDs of events generated in SSE scenarios. This can be useful in scenarios such as
UI composition, in which streams of markup events coming from different sources (e.g. different parts of a page)
can be sent to the browser combined in a single EventSource SSE stream. That way client JavaScript
code will be able to identify which part of the page the event belongs to by means of its prefix. Also, combining
several (prefixed) SSE streams into one can also serve to overcome limitations in the amount of concurrent
active EventSource allowed.
This interface also allows the specification of the first ID value to be used in these SSE events. This is useful in SSE scenarios in which the browser requests the server to reconnect after a connection failure, specifying the HTTP Last-Event-ID header so that the application can start generating events again starting from the event following the last one successfully processed by the browser (note this resume operation has to be supported by whoever is in charge of creating the data stream that Thymeleaf subscribes to in DATA-DRIVEN mode, not by Thymeleaf itself which is only in charge of rendering the view layer).
Returning SSE (Server-Sent Events) through Thymeleaf requires the presence of a variable implementing this interface in the context. Thymeleaf will generate three types of events during rendering:
Note that in the case of SSE, the value assigned to the IReactiveDataDriverContextVariable.getBufferSizeElements()
property does
affect the immediacy of the generated (published) events being sent to the browser. If this buffer is set e.g.
to 4, only when a total of 4 items of data are generated will be sent to the browser as SSE events.
The ReactiveDataDriverContextVariable
class contains a sensible implementation of this interface,
directly usable in most scenarios. Example use:
@RequestMapping("/something")
public String doSomething(final Model model) {
final Publisher<Item> data = ...; // This has to be MULTI-VALUED (e.g. Flux)
model.addAttribute("data", new ReactiveDataDriverContextVariable(data, 100, 1L)); // firstEventID = 1L
return "view";
}
Modifier and Type | Method and Description |
---|---|
long |
getSseEventsFirstID()
Returns the first value to be used as an id in the case this response is rendered as SSE
(Server-Sent Events) with content type text/event-stream.
|
String |
getSseEventsPrefix()
Returns the (optional) prefix to be used for SSE event names and IDs.
|
getBufferSizeElements, getDataStream
String getSseEventsPrefix()
Returns the (optional) prefix to be used for SSE event names and IDs.
Using a prefix for SSE events can be useful in scenarios such as UI composition, in which streams of markup events coming from different sources (e.g. different parts of a page) can be sent to the browser combined in a single EventSource SSE stream. That way client JavaScript code will be able to identify which part of the page the event belongs to by means of its prefix. Also, combining several (prefixed) SSE streams into one can also serve to overcome limitations in the amount of concurrent active EventSource allowed.
long getSseEventsFirstID()
Returns the first value to be used as an id in the case this response is rendered as SSE (Server-Sent Events) with content type text/event-stream.
After the first generated events, subsequent ones will be assigned an id by incrementing this first value.
Copyright © 2017 The THYMELEAF team. All rights reserved.