Sub topic 3
Polling, long polling, and WebSocket
This chapter compares three ways to push chat messages to a client. It assumes you know basic HTTP, understand what a persistent TCP connection is, and can read short JavaScript snippets.
The core problem
HTTP is request-driven. The client asks and the server answers. A chat system needs the reverse: when Bob sends a message, Alice's client must learn about it quickly, without Alice asking at the right moment. Each technique below is a different way to close that gap.
Short polling
The client sends a request every few seconds, such as "any new messages?". The server answers immediately, usually with an empty list.
- Simple to build and works through every proxy and firewall.
- Latency is up to one polling interval.
- Most requests return nothing, so cost grows with the number of clients, not the number of messages.
With 10 million connected users polling every 2 seconds, the servers handle 5 million requests per second, and almost all of them are empty.
Long polling
The client sends a request, and the server holds it open until a message arrives or a timeout (often 30 to 60 seconds) expires. The client then immediately sends the next request.
- Latency is close to real time.
- Empty responses are far fewer than with short polling.
- Each held request occupies a server resource, and every message costs a full HTTP request with headers.
- Messages that arrive between one response and the next request must be stored and returned on the next request.
- The sender and receiver may be connected to different servers, so the server holding the request may not be the one that received the message.
WebSocket
A WebSocket starts as an HTTP request with an Upgrade: websocket header. If the server agrees, the same TCP connection becomes a bidirectional channel. Either side can send a frame at any time, with only a few bytes of framing overhead.
Here is a minimal browser client that reconnects when the connection drops:
function connect() {
const ws = new WebSocket("wss://chat.example.com/ws");
ws.addEventListener("open", () => {
ws.send(JSON.stringify({ type: "hello" }));
});
ws.addEventListener("message", (event) => {
const data = JSON.parse(event.data);
console.log("received", data);
});
ws.addEventListener("close", () => {
setTimeout(connect, 1000);
});
}
connect();Comparison
| Property | Short polling | Long polling | WebSocket |
|---|---|---|---|
| Latency | Up to the interval | Near real time | Near real time |
| Server push | No | Simulated | Native |
| Per-message overhead | Full HTTP request | Full HTTP request | Small frame |
| Idle cost | High, from empty requests | Medium, from held requests | Low, one open socket |
| Server state | None | Short-lived | One long-lived connection per client |
| Proxy compatibility | Best | Good | Usually good, sometimes blocked |
What to choose for chat
Use WebSocket for message delivery. Chat is two-way and frequent, and the small per-message cost matters at scale. Keep a long-polling fallback only if you must support networks that block upgrades.
Ordinary request/response HTTP still fits operations that do not need push, such as login, profile edits, and loading old history. This split drives the architecture in the next chapter. Connections that stay open make the chat servers stateful, while the HTTP APIs can remain stateless.
Exercise
A chat app has 2 million active clients. Explain two reasons why short polling every 3 seconds is a poor choice, compared with WebSocket.
Show answer
First, load: 2 million clients every 3 seconds is about 667,000 requests per second, and nearly all return nothing, while each carries full HTTP headers. Second, latency: a message can wait up to 3 seconds before the client sees it, so the chat feels delayed. A WebSocket sends only when there is a message and delivers it immediately.