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.