← Lessons

quiz vs the machine

Gold1410

Networking

Long Polling vs WebSockets

Two ways to deliver near real time updates with different overhead.

4 min read · core · beat Gold to climb

The real time problem

Plain request and response cannot push data to a client when something changes. Long polling and WebSockets are two answers, with very different mechanics.

Long polling

In long polling the client sends a request and the server holds it open until data is ready, then responds. The client immediately sends another request.

  • The server delays the response until there is news.
  • Each update costs a full request and response cycle.
  • It works over plain HTTP with no special support.

WebSockets

A WebSocket upgrades a single HTTP connection into a persistent bidirectional channel. After the upgrade, either side can send messages any time with low overhead.

Choosing

Long polling is simple and proxy friendly but wastes a round trip per message and adds latency. WebSockets give true two way, low latency messaging at the cost of a persistent connection that proxies and load balancers must support. For chat and games WebSockets win, while occasional updates may be fine with long polling.

Key idea

Long polling fakes push with repeated held requests, while WebSockets open a real persistent bidirectional channel.

Check yourself

Answer to earn rating on the learn ladder.

1. How does long polling deliver an update?

2. What does a WebSocket provide that long polling does not?