Skip to content
matthewcm.dev

    ↑ ↓ move · ↵ open · tab scopeopen graph →

    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.

    Growingtended planted 2 min read
    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 a fetch-based stream reader if you need an Authorization header.
    • 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

    ↪ Links to (3)