All posts
Web Development · 8 min read · Jun 25, 2026

WebSockets vs HTTP polling

Real-time communication with WebSockets versus 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.

javascript
// Short polling: ask every few seconds
setInterval(async () => {
  const res = await fetch('/api/messages');
  const messages = await res.json();
  render(messages);
}, 3000);
Short polling is simple but sends requests whether or not data changed.

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.

javascript
const socket = new WebSocket('wss://example.com/chat');

socket.onopen = () => socket.send('hello');

socket.onmessage = (event) => {
  render(JSON.parse(event.data));
};
One persistent connection; the server can push messages instantly.

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

AspectPollingWebSockets
ConnectionNew request each timeSingle persistent connection
DirectionClient pulls onlyBoth directions (full-duplex)
LatencyUp to the poll intervalNear-instant
OverheadHigh (repeated requests)Low per message
ComplexityLowHigher
InfrastructureWorks everywhereNeeds 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.

Contact

Let’s connect

Have a Liferay or Backend project you’d like to talk through? Fill in the form and I’ll get back to you.

Book Calendar