Expand description
Server-Sent Events (SSE) response builder.
Sse assembles a complete text/event-stream response body from a
sequence of SseEvent values. The full body is buffered before any bytes
are written to the socket, which suits pre-known event sequences (batch
updates, enumerable progress steps, static push payloads).
For live streaming where events arrive over time (AI token output, real-time
sensor data), write text/event-stream headers and the raw event lines
directly to the TCP stream in a custom accept loop — the same approach used
for WebSocket connections.
§Wire format
Each event is a block of field: value\n lines terminated by a blank line:
id: 1
event: update
data: {"count":42}
Lines with no field name (starting with :) are comments and are ignored
by clients; they are used here as keep-alive pings.
§Example
use rust_web_server::sse::{Sse, SseEvent};
let response = Sse::new()
.event("connected", "ready")
.push(SseEvent::data(r#"{"count":1}"#).id("1").event_type("update"))
.push(SseEvent::data(r#"{"count":2}"#).id("2").event_type("update"))
.comment("keep-alive")
.into_response();