WebSockets vs server-sent events: picking the right realtime channel
SSE pushes one way over plain HTTP with automatic reconnect; WebSockets are bidirectional and stateful. A decision rule for chat, feeds, dashboards and streams.
When a page needs live updates - chat, dashboards, order tracking, collaborative editing - the choice usually lands on WebSockets or server-sent events (SSE). They are not interchangeable, and picking the simpler one that fits is a real engineering saving.
This guide compares the two using MDN's documentation, checked on 19 November 2026.
What server-sent events are
Traditionally a page must request data from the server; SSE flips that for one direction: the server can push new data to the page at any time. The browser's EventSource interface handles connecting to the server, receiving events and data, handling errors and closing the connection. SSE runs over plain HTTP as a long-lived text stream, which brings practical gifts: it works through proxies and HTTP infrastructure without special handling, the browser reconnects automatically when the connection drops, and the API is a few lines of client code. SSE is also available in Web Workers, so processing can stay off the main thread.
What WebSockets are
WebSockets upgrade an HTTP connection into a persistent, bidirectional channel: after the handshake, both client and server can send messages at any time, in either direction, with low framing overhead. That bidirectionality is the entire point - and the entire cost. WebSocket connections are stateful, sit outside normal HTTP request-response handling, and need explicit decisions about reconnection, heartbeats, backpressure and horizontal scaling (which server holds which connection).
The decision rule
- Server talks, client listens: use SSE. Live feeds, notifications, progress updates, streaming AI tokens, price ticks. One direction, HTTP-friendly, automatic reconnect, minimal code.
- Both sides talk freely: use WebSockets. Chat, multiplayer presence, collaborative cursors, anything where the client sends frequent low-latency messages. Paying the stateful-connection complexity is justified here.
- Occasional updates: consider neither. Plain polling on a sane interval is operationally trivial and often sufficient when "live" can mean "within thirty seconds".
Practical notes before shipping either
- Load balancers and timeouts. Long-lived connections need idle timeouts raised at every proxy hop, or users get silently disconnected on a timer.
- Connection counts are capacity. Each open connection costs server memory; size for concurrent connections, not requests per second.
- Fallbacks. Some networks interfere with WebSocket upgrades; SSE's plain-HTTP nature makes it more robust there, and both should degrade to polling when nothing persistent works.
- Auth on connect. Authenticate the connection at establishment and re-authorize long-lived sessions; a connection opened yesterday should not carry yesterday's permissions forever.
A note on scaling
Both options hold one open connection per online user, which changes your capacity math from "requests per second" to "concurrent connections". A single small server can hold thousands of mostly-idle SSE or WebSocket connections, but application servers with per-connection threads or workers will run out far sooner than the network does. If you expect real scale, check how your framework handles concurrent connections before choosing either - and remember that every reconnect storm (a deploy, a network blip) arrives as a burst of simultaneous connection attempts your stack must absorb.
Sources
- MDN Web Docs, Server-sent events: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events
Drafted with AI assistance from this source. Connection behavior varies across proxies, hosts and client networks; test long-lived connections through your real infrastructure before launch.