Interface IReactiveSSEDataDriverContextVariable
- All Superinterfaces:
IReactiveDataDriverContextVariable
- All Known Implementing Classes:
ReactiveDataDriverContextVariable
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:
- Header (
event: head
orevent: {prefix}_head
), a single event containing all the markup previous to the iterated data (if any). - Data message (
event: message
orevent: {prefix}_message
)), a series of n events, one for each value produced by the data driver. - Tail (
event: tail
orevent: {prefix}_tail
)), a single event containing all the markup following the last iterated piece of data (if any).
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";
}
- Since:
- 3.0.4
- Author:
- Daniel Fernández
-
Method Summary
Modifier and TypeMethodDescriptionlong
Returns the first value to be used as anid
in the case this response is rendered as SSE (Server-Sent Events) with content typetext/event-stream
.Returns the (optional) prefix to be used for SSE event names and IDs.Methods inherited from interface org.thymeleaf.spring5.context.webflux.IReactiveDataDriverContextVariable
getBufferSizeElements, getDataStream
-
Method Details
-
getSseEventsPrefix
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 activeEventSource
allowed.- Returns:
- the prefix to be applied to event names and IDs, or
null
if no prefix has been set. - Since:
- 3.0.8
-
getSseEventsFirstID
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 typetext/event-stream
.After the first generated events, subsequent ones will be assigned an
id
by incrementing this first value.- Returns:
- the first value to be used for returning the data driver variable values as SSE events.
-