Polling vs. WebSockets: Stop Asking, Start Listening

Polling is like repeatedly asking "Are we there yet?", while WebSockets is a persistent, two-way conversation. Use polling for infrequent updates, but use WebSockets for real-time apps like chat. The footgun is using polling for high-frequency updates.
Why it exists
The web's original HTTP protocol is based on a client request and server response. This is inefficient for real-time applications where the server has new data for the client. Constantly asking the server "Is there anything new?" (polling) creates significant overhead and latency. WebSockets were created to solve this by allowing the server to push data to the client at any time.
The mental model
HTTP Polling is like a child in a car repeatedly asking, "Are we there yet?". The client is solely responsible for initiating communication, sending a new request every few seconds to check for updates. This is inefficient and can be slow.
WebSockets are like an open phone line. After an initial handshake, the connection stays open, allowing both the client and server to talk to each other freely and instantly. It's a persistent, two-way conversation, not a series of one-off questions.
How it works
With polling, the client sends a standard HTTP request to an endpoint. The server checks for new data and sends a response, which might be empty. The client then waits for a set interval (e.g., 5 seconds) and repeats the entire process.
With WebSockets, the client sends a special HTTP request that includes an "Upgrade: websocket" header. If the server supports it, it completes a handshake, and the connection is upgraded from HTTP to a persistent WebSocket connection. From then on, both sides can send data frames directly over the underlying TCP connection without the overhead of new HTTP headers for each message.
When to use it
Use polling for simple use cases where updates are infrequent and some latency is acceptable, or when you must work with simple, stateless server infrastructure. Use WebSockets for any application that needs true real-time, low-latency communication. This includes chat applications, live activity feeds, real-time sports scores, multiplayer browser games, and collaborative editing tools.
When not to use it
Avoid polling for any high-frequency or low-latency requirement; it generates excessive network traffic and server load. WebSockets can be overkill for data that changes very infrequently. They also require servers to maintain a persistent connection for each client, which introduces statefulness and can complicate scaling compared to stateless HTTP servers.
One canonical example
A live chat application. Using polling, every client would need to send a request to the server every second to check for new messages. With 1,000 users, that's 1,000 requests per second just to check for updates. With WebSockets, there are 1,000 open connections. When one user sends a message, the server instantly pushes it to the other clients over their existing connections with minimal overhead.
Interview question
For a live chat application requiring instant message delivery, which communication method is most appropriate?
- a.WebSockets, because they eliminate the need for an initial handshake, speeding up connection setup.
- b.Polling, because it reduces network traffic by only sending data when the client requests it.
- c.Polling, as it uses standard HTTP requests which are simpler for stateless servers.
- d.WebSockets, because they establish a persistent, two-way connection for real-time data push.Correct
Why? this is the answer
WebSockets are ideal for real-time applications like live chat because they create a persistent, two-way connection, allowing the server to push updates instantly. Polling (option B) would generate excessive network traffic and server load if used for frequent, instant updates, making it inefficient.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #http
- #websockets
- #networking
- #real-time
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on http — each one lists the topics its interview covers.
See open roles