WebSocket vs Polling
date
Jan 21, 2024
slug
web-sockets-polling
status
Published
tags
CONCEPTS
summary
type
Post
Why is HTTP alone not sufficient for real-time applications like chat applications?
Web servers typically receive HTTP requests and respond with HTTP responses. While this suffices for many applications, it falls short for real-time applications, such as a chat application.
Consider the scenario where USER A wishes to send a message to USER B. User A can transmit the message as an HTTP POST request, encapsulating the message details within the request body to the server. However, a challenge arises in delivering this message to USER B. As mentioned, the server is capable of handling incoming HTTP requests but lacks the ability to initiate communication with the client.
In simpler terms, the server cannot initiate a TCP connection with the client on its own; the client always takes the initiative in initiating connections. Client makes the first move.
Is Polling Enough for Chat Application?
One clear solution to the above problem is for USER B to periodically send HTTP requests to the Server, checking if there are any messages intended for USER B. If other users have sent messages directed towards USER B, the Server can then transmit those messages to USER B as an HTTP response.

Let’s crunch some numbers.
Consider the potential server load for 1 million users.
- In the event of all 1 million users sending requests simultaneously, the server would need to process 1 million requests per second.
- Even if there are no messages for clients, clients have to keep sending requests to server infrastructure every x seconds.
- In addition every HTTP request have a certain amount of overhead because of TCP handshake.
- Furthermore, HTTP protocol is verbose, there are lots of data that needs to be transmitted such as headers and so on which seems unnecessary for simply relaying messages.
This poses a substantial load on the server infrastructure and may lead to performance issues, latency, or even server overload.
SOLUTION, WebSockets?
WebSockets enable bidirectional communication between the server and clients, allowing for instant message delivery without the need for constant polling.

This means, clients and server can communicate freely without need for constant HTTP requests.