Short Polling vs Long Polling
Before proceeding, I am assuming, you are aware of the request-response architecture of a basic web application. To build a real-time application like a chat application we can't use the basic request-response architecture, but we can use it with a
polling mechanism to achieve the real-time behaviour. Apart from the polling mechanism we also have
SSE(server-side event) and
WebSocket to achieve real-time behaviour.
In this article, I am going to discuss two types of polling techniques i.e,
long polling and
First, we need to know, what is
Polling is a technique in which the client sends a request to the server asking for data in an interval of time.
The response from the server can be empty or any kind of data.
Short Polling is a technique in which the client sends a request to the server asking for data at fixed delays after getting a response from the previously sent request.
- Client sends a request to the Server.
- Server responds with an empty response or data, if available.
- Client will wait for the specified delay after receiving the response and continues the same request-response process again.
This technique is very simple and doesn't consume server resources, but event notifications are not so instant and there will be some delay.
A client sends a request to the server asking for data, but the data is not available at that time and the server responds with an empty response.
The client will wait for 5 seconds before sending the next request. That means if, in that interval of time (5 seconds), any data is available the client will not be notified about this.
The client will be able to get the data when it sends the next request.
So this technique is not so instant, and there will some delay.
Long Polling is a technique in which the client sends a request to the server asking for data, but the server doesn't respond instantly if no data is available, rather it waits for a specific amount of time. If in that interval of time, any event happens or data become available, the server will respond with that data and in case of no events or data, the server will respond with an empty response after the specified timeout.
This technique is more complex and does consume server resources, but it can notify the client without any delay so it can give a better real-time experience.
Thank you for reading 🙏
If you enjoyed this article or found it helpful, give it a thumbs-up 👍
Feel free to connect 👋
Interested in reading more such articles from Bibek Kakati?
Support the author by donating an amount of your choice.