Guide

WebSockets, Server-Sent Events or polling: choosing a real-time transport

Published · 8 minute read

"Real-time" covers very different needs: a dashboard that refreshes every few seconds, a notification that should appear promptly, a chat where both sides type, a model's answer streaming in word by word. WebSockets are often chosen by reflex for all of them. They are the most capable option and the most expensive to operate, and for many of these jobs something simpler does better.

Four ways to get fresh data to a browser

Polling

The client asks on a timer: "anything new?". It is plain HTTP, works through every proxy, is trivially cacheable and needs no special server. The costs are delay, on average half the interval, and waste, since most responses say "no". Polling is the right answer more often than its reputation suggests: for data that changes every few minutes, or when a few seconds of delay is acceptable, nothing else is as easy to run.

Long polling

The client asks, and the server holds the request open until it has something to say or a timeout passes. The client then immediately asks again. Updates arrive promptly without a constant stream of empty responses. It is still ordinary HTTP, at the price of one held connection per client and a little reconnection logic.

Server-Sent Events

The client opens one HTTP request and the server keeps the response open, writing events to it as they happen. The browser's EventSource API handles the rest:

const source = new EventSource("/events");
source.onmessage = (e) => render(JSON.parse(e.data));

On the wire it is a text format with a content type of text/event-stream:

id: 42
event: price
data: {"symbol":"ABC","value":101.5}

A blank line ends each event. SSE flows in one direction only, from server to client.

WebSockets

A WebSocket begins as an HTTP request that asks to upgrade the connection. After that it is no longer HTTP: it is a persistent, two-way channel carrying text or binary messages in either direction with very little overhead per message.

What SSE gives you for free

SSE is often overlooked, which is a pity because it solves the commonest case, a server pushing updates to a page, with almost no code.

Its limits are real, though:

What WebSockets cost

A WebSocket gives you a raw message pipe and leaves the rest to you:

A decision guide

You are buildingReasonable choice
A dashboard where data changes every minutePolling
Notifications, activity feeds, progress of a background jobSSE
Streaming a generated response to the pageSSE or a streamed fetch
Live prices or scores, one way, many updatesSSE
Chat with typing indicators and presenceWebSocket
Collaborative editing, multiplayer gamesWebSocket
High-frequency or binary data in both directionsWebSocket

The deciding question is whether the client needs to send frequent, low-latency messages on the same channel. If the client mostly listens and occasionally acts, SSE plus ordinary requests is simpler and more robust. If both sides talk continuously, use a WebSocket.

Whichever you choose

You can watch both protocols at work with the WebSocket Tester and the SSE Client: connect to an endpoint, see each message as it arrives, and observe what happens when the connection drops.

Tools used in this guide

More guides