1.Polling, long polling, SSE and WebSockets
These are the four standard ways a client gets near-real-time data from a server. Polling repeats a plain request every few seconds regardless of whether anything changed. Long polling holds each request open until the server actually has something to say, then the client immediately reopens it. Server-Sent Events (SSE) is a single long-lived HTTP connection the server keeps pushing text events down, one direction only, with the browser reconnecting automatically if it drops. WebSockets upgrade one HTTP handshake into a persistent, full-duplex socket where either side can send a message at any time. Each option trades implementation simplicity and infrastructure compatibility for how immediate and how bidirectional the communication can be.
Key features
- Polling: a fresh HTTP request on a fixed interval — simplest to build and debug, but wastes requests when nothing changed and adds up to interval-length latency
- Long polling: the server holds the request open (no response) until it has data or a timeout hits, then the client reopens immediately — lower latency than polling, but still one request-response cycle per message, and each open request ties up a server thread/connection
- SSE: one persistent HTTP connection, server-to-client only, text-based (the EventSource API), with automatic reconnection built into the browser client — no custom reconnect logic needed
- WebSockets: one HTTP handshake upgrades the TCP connection to a full-duplex socket (ws:// or wss://) — either side can push a message at any time, framed as messages rather than HTTP requests
- Going down the list — polling to long polling to SSE to WebSockets — each step trades more server-side connection state and infrastructure complexity for lower latency and (for WebSockets) two-way communication
The four transports compared
- Polling — fixed interval, always wasteful
- Long polling — held open until data arrives
- SSE — server push, one direction
- WebSockets — full-duplex, most infra cost
Command
// SSE — server-to-client only, browser auto-reconnects
const es = new EventSource('/stream');
es.onmessage = (e) => console.log(e.data);
// WebSocket — full-duplex, single connection
const ws = new WebSocket('wss://example.com/ws');
ws.onmessage = (e) => console.log(e.data);
ws.send('client can push too');Polling: simplest, wastes requests, laggy. Long polling: lower latency, one request per message, ties up a server slot while waiting. SSE: server push over plain HTTP, one direction, auto-reconnect built in. WebSockets: full-duplex, needs upgrade support everywhere in the path, most infrastructure cost.