Server-Sent Events (SSEs)
One-way server-to-client streaming over plain HTTP, with automatic reconnection built into the browser. The simplest right answer for feeds, progress updates and streaming AI responses.
On this page
Server-Sent Events give you a long-lived HTTP response that the server keeps writing to. The client listens; if it wants to send something back, it uses an ordinary request. That one-way restriction sounds limiting, but a surprising number of “real-time” features only need the server to talk.
What it looks like
The server responds with Content-Type: text/event-stream and writes small text frames:
HTTP/1.1 200 OK
Content-Type: text/event-stream
id: 42
event: progress
data: {"pct": 60}
And the browser has a built-in client:
const es = new EventSource("/jobs/123/events");
es.addEventListener("progress", (e) => setProgress(JSON.parse(e.data).pct));
EventSource reconnects automatically and sends the last event ID it saw (Last-Event-ID), so the server can replay what
was missed. With WebSockets you write all of that yourself.
A pattern I like: respond fast, enrich later
A user submits something that takes a while to process — say, an idea that needs analysing. Return a quick response immediately, then stream updates over SSE as background work finishes. The UI feels instant and gets richer as results arrive. It’s also exactly how most LLM chat interfaces stream tokens.
Gotchas
- Text only. Binary has to be encoded.
- No custom headers on
EventSource. Use cookies, or afetch-based stream reader if you need anAuthorizationheader. - Connection limits. Under HTTP/1.1, browsers allow around six connections per domain, which several open tabs can exhaust. HTTP/2 multiplexes streams and makes this go away.
- Buffering proxies can hold the stream back. Disable response buffering, and send a comment line (
: ping) every so often as a Heartbeat so idle timeouts don’t kill it.
Streaming from physical devices
On a network automation platform I built an SSE stream from physical network devices to the web app: live polling logs and command output. The data only flowed one way, continuously, so SSE fitted: plain HTTP (fine through proxies and gateways), automatic reconnect, and none of the connection state a WebSocket would add. The alternative, polling REST endpoints and shipping the same JSON over and over, would have been the bottleneck.
Choosing
Why SSE over WebSockets so often? WebSockets are harder to implement and to scale, because every connection is stateful. SSE is native in the browser and is plain HTTP: one request, then the server keeps sending messages back down it. It travels through existing load balancers, auth and HTTP/2 without special treatment.
If the client only needs to receive, start with SSE. Reach for WebSockets when the client also sends frequently on the same channel. SSE is essentially the standardised, browser-supported version of HTTP Long Polling. The full comparison with polling and WebSockets, including the offline case, is in WebSockets.
- #sse
- #real-time
- #streaming
- #http
↩ Linked from (8)
- AJAX PollingGrowing
…| | HTTP Long Polling | server answers when ready | low | held connections | fallback, legacy, job queues | | Server-Sent Events (SSEs) | server → client | low | one connection per client | feeds, progress, notifications…
…logs and command output from physical devices reached the browser over SSE instead of repeated REST polling (Server-Sent Events (SSEs)). A typical workflow the platform made repeatable: IPv4 Exhaustion and IPv6 Translat…
Related: WebSockets · Server-Sent Events (SSEs) · Message Delivery Semantics · REST API Design Trade-offs: Bulk Deletes, Request Bodies and Cancellation
- HeartbeatGrowing
…hem up is a classic way to cause a restart loop. Long-lived connections: WebSockets use ping/pong frames, and Server-Sent Events (SSEs) streams send comment lines, both to detect dead peers and to stop proxies culling an…
- HTTP Long PollingGrowing
…send a cursor (?since=…) so the server can replay anything missed. This is the same idea as Last-Event-ID in Server-Sent Events (SSEs). Server resources. Every waiting client holds a connection. That's cheap with async…
- Message PassingGrowing
…ve message handling. Distributed systems: queues, event streams and real-time channels such as WebSockets and Server-Sent Events (SSEs) are message passing between machines.
Related: WebSockets · Server-Sent Events (SSEs) · Finding React Re-render and Memory Leaks
- WebSocketsEvergreen
…| WebSockets | | Near-real-time, only the server pushes (feeds, notifications, progress, LLM token streams) | Server-Sent Events (SSEs) — simpler, plain HTTP | | Updates every few seconds or minutes are fine | AJAX Polli…
↪ Links to (3)
Related by topic
- Seed
Flyweight Views over Byte Buffers
Backend - Seed
requestAnimationFrame and High Refresh Rates
Frontend craft - Seed
Rewrites Are Now a Business Case
Backend - Growingcase study
Smart TV Apps on Low-End Devices
Frontend craft