WebSockets vs HTTP polling
Many applications need to show data that changes while the user is looking at it: a chat message arrives, a stock price ticks, an order status updates. HTTP was designed around a request-response model where the client always initiates, so delivering real-time updates requires a strategy. The two main options are HTTP polling and WebSockets, and they sit at opposite ends of the trade-off spectrum.
HTTP polling
With polling, the client repeatedly asks the server whether anything has changed. In its simplest form, it sends a request on a fixed interval.
// Short polling: ask every few seconds
setInterval(async () => {
const res = await fetch('/api/messages');
const messages = await res.json();
render(messages);
}, 3000);Polling is easy to implement and works everywhere because it uses ordinary HTTP. The downside is efficiency: most requests return no new data, wasting bandwidth and server capacity, and updates arrive only as fast as the interval allows.
Long polling
Long polling improves on this by having the server hold the request open until new data is available, then respond. The client immediately opens another request. This reduces empty responses and lowers latency, but it ties up server connections and is still fundamentally a series of HTTP requests.
WebSockets
WebSockets establish a single, persistent, full-duplex connection between client and server. After an initial HTTP handshake, the connection upgrades to the WebSocket protocol, and either side can send messages at any time without re-establishing a connection.
const socket = new WebSocket('wss://example.com/chat');
socket.onopen = () => socket.send('hello');
socket.onmessage = (event) => {
render(JSON.parse(event.data));
};WebSockets deliver true real-time, low-latency, bidirectional communication with minimal per-message overhead. The cost is added complexity: you need to manage connection lifecycle, reconnection, scaling across servers, and fallbacks for environments that block them.
Comparing the approaches
| Aspect | Polling | WebSockets |
|---|---|---|
| Connection | New request each time | Single persistent connection |
| Direction | Client pulls only | Both directions (full-duplex) |
| Latency | Up to the poll interval | Near-instant |
| Overhead | High (repeated requests) | Low per message |
| Complexity | Low | Higher |
| Infrastructure | Works everywhere | Needs WS support and scaling |
When to use each
- Use polling when updates are infrequent, latency is not critical, or you want the simplest possible setup.
- Use long polling as a middle ground when you need lower latency but cannot use WebSockets.
- Use WebSockets for chat, collaborative editing, live dashboards, gaming, and anything that needs instant, two-way updates.
- Consider Server-Sent Events (SSE) when you only need one-way, server-to-client streaming over plain HTTP.
Conclusion
Polling is the pragmatic choice for occasional updates where simplicity wins. WebSockets are the right tool when you need genuine real-time, bidirectional communication and are willing to manage the extra infrastructure. Match the technique to how often your data changes and how quickly users need to see it - over-engineering a notification badge with WebSockets is just as wasteful as polling every second for a live chat.