tezvyn:

Polling vs. WebSockets: Stop Asking, Start Listening

AI-drafted, machine-checkedSource: developer.mozilla.orgbeginner
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.

Read the original → developer.mozilla.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.