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. A null field 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 into T.
final
event → String?
The event: name, such as order-created; null for an unnamed event, which the protocol calls message.
final
hashCode → int
The hash code for this object.
no setteroverride
id → String?
The event's id:, or null when it has none. After a lost connection a client sends the last id it saw back in the Last-Event-ID header, 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-stream format, ready to write to a response: comment, id:, event:, retry: and data: 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 data and 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