WebSocket, SSE or long polling: which to choose
If data flows only from the server, SSE is enough, while intensive two-way interaction needs WebSocket. SSE is plain HTTP, and EventSource reconnects by itself and sends Last-Event-ID, so resuming from the point of the drop is almost free. With WebSocket the application handles reconnection, authorisation and catch-up. Polling stays for rare updates, and buffering proxies break SSE.
Networking · Real-time
An import progress bar must update live. WebSocket, SSE or just polling?
The product owner asks to show the progress of a long import live. A developer at once proposes WebSocket, because it is "for real time". But data flows only from the server to the client, and the client sends nothing. In that case WebSocket brings more complexity than benefit. The lesson compares four ways, shows how they differ and why the choice affects testing.
How it works and why. All four solve one thing: show the user a change on the server without a reload. They differ in who initiates the exchange and how much each event costs.
Four ways of real-time updates
- Short polling: the client asks every second. Simple, but wastes requests and adds a delay of up to the interval.
- Long polling: a request hangs until an event appears (or a timeout passes), and after the response the client immediately makes a new one. Works wherever HTTP works, but every event costs a request with headers.
- SSE (Server-Sent Events): one HTTP request with
Content-Type: text/event-stream, the server streams events. Server to client only, text only. The browserEventSourcereconnects itself and sendsLast-Event-ID, so resuming from the break point is almost free. - WebSocket: two-way, text and binary, low latency, but its own protocol: reconnection, authorization, catching up and balancing of sticky connections are done by the application.
SSE versus WebSocket
SSE
Plain HTTP, server → client only, text only. The browser reconnects itself and sends Last-Event-ID. Passes proxies without special handling.
WebSocket
Its own protocol, two-way, text and binary, low latency. Reconnection, authorization and catching up are the application task.
The rule of choice. Data flows only from the server (notifications, progress, quotes, an LLM answer stream): SSE, and it is enough. Intensive two-way interaction (chat, shared editing, games, trading terminals): WebSocket. Rare updates and strict network limits: polling. Long polling remains a fallback when nothing else is available.
Example: import progress over SSE. This is what the stream looks like on the wire. The fields are id, event, data, and an empty line ends an event:
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
id: 41
event: progress
data: {"percent":40}
id: 42
event: progress
data: {"percent":80}
The implementation on the server and the client is minimal. Note X-Accel-Buffering: no: without it nginx may buffer the response, and all events appear in one chunk at the end, as if SSE "did not work":
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
// nginx must not buffer the response
'X-Accel-Buffering': 'no',
});
res.write('id: 41\nevent: progress\ndata: {"percent":40}\n\n');
// client
const es = new EventSource('/api/jobs/42/events');
es.addEventListener('progress', (e) => update(JSON.parse(e.data)));
If the connection drops, the browser opens it again and sends Last-Event-ID: 42, and the server continues from event 43. What you have to write yourself for WebSocket is already in the protocol here.
For testing the difference is significant. SSE is an ordinary HTTP response that can be intercepted and replaced as a response (route.fulfill with a text/event-stream body), and its body can be checked with curl -N. WebSocket needs separate APIs: see the lesson on testing WebSocket. When testing SSE, separately check behaviour on a drop, the presence of id and that the client does not duplicate an event on a retry.
Limits and alternatives. EventSource can only do GET and allows no custom headers, so for POST or Authorization people use fetch with body streaming (this is how streaming answers of LLM services work) and parse the format themselves. On HTTP/1.1 a limit of ~6 connections per domain applies, so several tabs with SSE can exhaust it; HTTP/2 multiplexes streams, so the problem does not exist. Proxies and CDNs that buffer the response break both SSE and long polling. Response compression can also accumulate events, so it is turned off for a stream.
Common mistakes.
- Picking WebSocket "because it is real time" when data flows one way: extra reconnection, authorization, balancing.
- Not switching off proxy buffering for SSE: events arrive in a batch, not one by one.
- Not sending
idin events: after a drop, catching up on what was missed is impossible. - Polling too often: each response is a full HTTP request with headers, and the load grows linearly with the number of clients.
- Forgetting that a long connection with no traffic is closed by a proxy on idle: a periodic comment or heartbeat is needed.
Check yourself
A notification feed only reads events from the server. Which way do you choose and which two arguments do you give?
Answer
SSE. Data flows only from the server, and EventSource reconnects itself and sends Last-Event-ID, so catching up from the break point works out of the box. It is plain HTTP that passes proxies and balancers without special handling, and in tests it can be intercepted as a response. WebSocket would add complexity here with no need for a two-way channel.
SSE works on the stand, but behind nginx all events arrive together after a minute. What is the cause and how do you check?
Answer
The proxy buffers the response and releases it when the buffer fills or the connection closes. Buffering is switched off (proxy_buffering off for this location, or an X-Accel-Buffering: no header from the application) and checked with curl -N: events must appear one by one right after being sent.