Class ServerSentEvent
ServerSentEvent object describes a single server-sent event that
can be sent through an SseConnection or broadcast to all the
connections of an SseBroadcaster.
All the fields are optional, since they will only be part of the event on the wire when they have been set. Events are built fluently:
sse.send(new ServerSentEvent().name("update").id("42").data("changed"));
Data can be provided as text with data(), or as a
Template or a single template block with template() and
templateBlock(). Templates will be processed exactly like
Context.print(Template) processes them, with all the filtered
tags resolved against the context of each receiving connection, which
makes them convenient for sending HTML fragments, for instance for the
htmx sse extension.
- Since:
- 1.10
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a comment line to the event.data(CharSequence data) Adds data to the event, which is transmitted asdatafields.Sets the event ID, which is transmitted as theidfield.Adds the compact JSON representation of a value as the data of the event, which is transmitted as adatafield.Sets the event name, which is transmitted as theeventfield.retry(int milliseconds) Sets the reconnection time in milliseconds, which is transmitted as theretryfield.Uses the content of a template as the data of the event.templateBlock(Template template, String blockId) Uses the content of a single template block as the data of the event.
-
Constructor Details
-
ServerSentEvent
public ServerSentEvent()
-
-
Method Details
-
name
Sets the event name, which is transmitted as theeventfield.Browsers dispatch named events to the listener that was registered for that particular name.
- Parameters:
name- the name of the event- Returns:
- this event instance
- Throws:
IllegalArgumentException- when the name contains line breaks- Since:
- 1.10
- See Also:
-
id
Sets the event ID, which is transmitted as theidfield.Browsers echo the last received ID back in the
Last-Event-IDheader when they reconnect, which can be retrieved withSseConnection.lastEventId().Event IDs that you set yourself are the reconnection strategy of your application. Broadcasters that have
historyenabled assign their own IDs and will refuse events that carry one.- Parameters:
id- the ID of the event- Returns:
- this event instance
- Throws:
IllegalArgumentException- when the ID contains line breaks or NUL characters, since browsers ignore IDs with NUL characters when they track their last event ID- Since:
- 1.10
- See Also:
-
retry
Sets the reconnection time in milliseconds, which is transmitted as theretryfield.- Parameters:
milliseconds- the reconnection time in milliseconds- Returns:
- this event instance
- Throws:
IllegalArgumentException- when the reconnection time is negative- Since:
- 1.10
-
comment
Adds a comment line to the event.Comments are ignored by browsers and are typically used as keep-alive heartbeats to prevent proxies from closing idle connections.
- Parameters:
comment- the comment text- Returns:
- this event instance
- Since:
- 1.10
- See Also:
-
data
Adds data to the event, which is transmitted asdatafields.This method can be called multiple times, since each call adds another line of data. Data that itself contains line breaks will automatically be transmitted as multiple
datafields.- Parameters:
data- the data to add- Returns:
- this event instance
- Since:
- 1.10
- See Also:
-
json
Adds the compact JSON representation of a value as the data of the event, which is transmitted as adatafield.This accepts the same values as
Json.toString(Object): strings, numbers, booleans,null, maps, collections, arrays, dates, temporals, enums,JsonObjectandJsonArray. Beans and records are additionally converted throughJson.from(java.lang.Object), as are the elements of collections.Since compact JSON contains no line breaks, the value will always be transmitted as a single
datafield, which clients can parse directly withJSON.parse(event.data).- Parameters:
value- the value whose JSON representation will be added as event data- Returns:
- this event instance
- Since:
- 1.10
- See Also:
-
template
Uses the content of a template as the data of the event.The template will be processed exactly like
Context.print(Template)processes it, resolving all the filtered tags against the context of each connection that receives the event.A single trailing line break of the template content isn't transmitted, since it's a template file convention rather than meaningful event data.
- Parameters:
template- the template whose content will be used as event data- Returns:
- this event instance
- Since:
- 1.10
- See Also:
-
templateBlock
Uses the content of a single template block as the data of the event.The template will be processed exactly like
Context.print(Template)processes it, after which only the content of the provided block is used, with the value assignments that are active at that moment.This makes it possible to maintain a page template and the event fragments that update it in the same template file.
- Parameters:
template- the template that contains the blockblockId- the ID of the block whose content will be used as event data- Returns:
- this event instance
- Since:
- 1.10
- See Also:
-