SSE Event Stream
Server-Sent Events (SSE) is a server-push technology built on plain HTTP that enables a server to stream a sequence of events to a client over a single long-lived connection, using the text/event-stream content type.
Server-Sent Events (SSE) is a server-push technology built on plain HTTP that enables a server to stream a sequence of events to a client over a single long-lived connection, using the text/event-stream content type.
SSE occupies a different niche than WebSockets. WebSockets provide full-duplex communication; SSE is strictly server-to-client. This constraint makes SSE simpler to implement, works over standard HTTP/2 without upgrade negotiation, and is compatible with HTTP caching proxies and firewalls that might block WebSocket upgrades.
Connection Setup: The client makes a standard HTTP GET request with Accept: text/event-stream. The server responds with Content-Type: text/event-stream and Cache-Control: no-cache, keeping the connection open indefinitely.
Event Format: Events are plain text, separated by blank lines. Each event can have an optional id: field (used for reconnection), an optional event: field (custom event type), a data: field (the payload, can be multi-line), and an optional retry: field (reconnection interval in ms).
Automatic Reconnection: The browser's EventSource API automatically reconnects if the connection drops. On reconnect, it sends the Last-Event-ID header with the ID of the last received event, allowing the server to resume the stream from the correct position.
HTTP/2 Multiplexing: Unlike WebSockets (which require their own connection), SSE streams can be multiplexed over an HTTP/2 connection alongside normal requests. This allows many SSE streams with lower overhead per stream.
SSE is ideal for one-directional real-time updates: live dashboards, build status feeds, AI chat token streaming, stock tickers, and notification feeds. For bidirectional communication, prefer WebSockets.