Class SseConnection
- All Implemented Interfaces:
AutoCloseable
SseConnection instance represents a single server-sent events
(SSE) connection to a client.
An SSE connection is obtained inside an element through
Context.sse() or Context.sse(SseBroadcaster). Creating it
prepares the response for event streaming: the content type is set to
text/event-stream, response buffering is disabled, and the
headers are flushed to the client so that it knows that the stream has
been established. Every event that is sent afterwards will immediately be
flushed to the client.
The first variant keeps the element in control of the connection, which stays open for as long as the element is executing:
get("/events", c -> {
var sse = c.sse();
while (sse.isOpen()) {
sse.send(new ServerSentEvent().name("tick").template(c.template("tick_fragment")));
// ...
}
});
The second variant registers the connection with an
SseBroadcaster and detaches it from the element, which returns
immediately while the connection stays open. Events can then be pushed
from anywhere in the application through the broadcaster.
Sending an event to a client that has disconnected will mark the
connection as closed, which can be detected with isOpen().
- Since:
- 1.10
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe content type that is used for server-sent events streams. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this connection.booleanSends a comment line, typically used as a keep-alive heartbeat.context()Retrieves the context of the request that established this connection.booleanisOpen()Indicates whether this connection is still open.Retrieves the ID of the last event that the client received.booleansend(CharSequence data) Sends an event with the provided text as its data.booleansend(ServerSentEvent event) Sends an event to the client of this connection.booleanSends an event with the content of the provided template as its data.booleanSends an event with the content of a single template block as its data.
-
Field Details
-
CONTENT_TYPE_EVENT_STREAM
The content type that is used for server-sent events streams.- Since:
- 1.10
- See Also:
-
-
Method Details
-
send
Sends an event to the client of this connection.The event will immediately be flushed. When the client has disconnected, the connection is marked as closed and
falseis returned.- Parameters:
event- the event to send- Returns:
truewhen the event was sent successfully; orfalsewhen the connection was closed- Since:
- 1.10
- See Also:
-
send
Sends an event with the provided text as its data.- Parameters:
data- the data of the event- Returns:
truewhen the event was sent successfully; orfalsewhen the connection was closed- Since:
- 1.10
- See Also:
-
send
Sends an event with the content of the provided template as its data.The filtered tags of the template will be resolved against the context of this connection.
- Parameters:
template- the template whose content will be used as event data- Returns:
truewhen the event was sent successfully; orfalsewhen the connection was closed- Since:
- 1.10
- See Also:
-
send
Sends an event with the content of a single template block as its data.The filtered tags of the template will be resolved against the context of this connection.
- Parameters:
template- the template that contains the blockblockId- the ID of the block whose content will be used as event data- Returns:
truewhen the event was sent successfully; orfalsewhen the connection was closed- Since:
- 1.10
- See Also:
-
comment
Sends a comment line, typically used as a keep-alive heartbeat.- Parameters:
comment- the comment text- Returns:
truewhen the comment was sent successfully; orfalsewhen the connection was closed- Since:
- 1.10
- See Also:
-
lastEventId
Retrieves the ID of the last event that the client received.Browsers transmit this through the
Last-Event-IDrequest header when they reconnect. When that header is absent, thelastEventIdrequest parameter is used instead, which makes it possible for pages to provide the ID of the last event that was rendered as part of the stream URL of the initial connection.- Returns:
- the last event ID; or
nullwhen the client didn't provide one - Since:
- 1.10
- See Also:
-
isOpen
public boolean isOpen()Indicates whether this connection is still open.A disconnect is detected when the servlet container reports that the asynchronous request has completed, timed out or errored, and otherwise when sending an event to the client fails. This means that a stale connection can still report being open until the container notices, or until the next event or
heartbeatis sent.- Returns:
truewhen the connection is open; orfalsewhen it has been closed- Since:
- 1.10
- See Also:
-
context
Retrieves the context of the request that established this connection.- Returns:
- this connection's context
- Since:
- 1.10
- See Also:
-
close
public void close()Closes this connection.For detached connections, this will complete the underlying asynchronous request and end the response.
- Specified by:
closein interfaceAutoCloseable- Since:
- 1.10
- See Also:
-