ServerSentEvent<T> class
final
One server-sent event, the unit of a text/event-stream response: its
data, plus an optional id, event name, retry delay and
comment.
T is the type of the data: String as it travels, or a class that
is written to and read from JSON. The extra fields are what the
protocol adds to plain data. The event name lets one stream carry
several kinds of messages. The id lets a client resume after a lost
connection, since it sends the last id back in Last-Event-ID. The
retry delay tells the client how long to wait before reconnecting.
You need this type only when those fields matter. A client reading an
EventStream<T> gets the data directly and finds the whole events in
EventStream.events. A server writes the data of a plain Stream<T>
or EventStream<T> as events by itself; it emits a ServerSentEvent
(in a stream or through an SseEmitter) to set a name or an id.
// Server: name each event so the client can tell the kinds apart, and
// give it an id so a reconnecting client can resume after it.
@GetMapping('/orders/events')
Stream<ServerSentEvent<Order>> events() => orders.changes.map(
(order) => ServerSentEvent(order, id: order.id, event: 'order-updated'),
);
// Client: a Feign method returning EventStream<Order>.
client.events().events.listen((ServerSentEvent<Order> e) {
log.info('${e.id} ${e.event}: ${e.data.status}');
});
Spring (Java): ServerSentEvent<T>, built with a builder on the server
and read with WebClient on the client:
ServerSentEvent.builder(order).id(order.id()).event("order-updated")
.build();
Flux<ServerSentEvent<Order>> events = webClient.get()
.uri("/orders/events")
.retrieve()
.bodyToFlux(new ParameterizedTypeReference<>() {});
Constructors
- ServerSentEvent(T data, {String? id, String? event, Duration? retry, String? comment})
-
An event carrying
data, with the other fields when given. Anullfield is left out when the event is written.const
Properties
- comment → String?
-
The comment lines (lines starting with
:), joined with newlines. Clients ignore them; servers send them as keep-alive heartbeats.final - data → T
-
The payload. On the wire it is the event's
data:lines, joined with newlines; a client then decodes that text intoT.final - event → String?
-
The
event:name, such asorder-created;nullfor an unnamed event, which the protocol callsmessage.final - hashCode → int
-
The hash code for this object.
no setteroverride
- id → String?
-
The event's
id:, ornullwhen it has none. After a lost connection a client sends the last id it saw back in theLast-Event-IDheader, so the server can resume after it.final - retry → Duration?
-
The
retry:delay: how long the server asks clients to wait before reconnecting. Sent in whole milliseconds.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
encode(
[String text(T data)?]) → String -
This event in the
text/event-streamformat, ready to write to a response: comment,id:,event:,retry:anddata:lines, then the blank line that ends the event. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
override
-
withData<
R> (R data) → ServerSentEvent< R> -
This event with other
dataand the same id, name, retry and comment: for example the decoded object in place of the received text.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override