![]() |
VOOZH | about |
Polling is a technique where a client repeatedly sends requests to a server at fixed intervals to check for new data. It is widely used to simulate real-time updates when instant communication is not strictly required. Although it is not truly real-time, polling is simple to implement and works reliably in many applications.
Example: A news app refreshing every 10 seconds to fetch the latest headlines from the server.
Short polling is a technique where the client continuously sends requests to the server at fixed intervals. The server immediately respondsโeither with data if available or an empty response if not. This cycle keeps repeating, allowing the client to check for updates regularly.
Example: A chat application that sends a request to the server every 5 seconds to check for new messages, even if no new messages have arrived.
In this example, we took the server file as data.json which contains some data.
data.json:
{
"name":"Manish",
"age":"22",
"city":"Kolkata"
}
Output:
๐ ShortPoolingShort polling works best in scenarios where updates are not critical and can tolerate some delay. Common use cases:
These highlight why short polling is easy to use but may not be efficient.
These show the limitations mainly related to performance and resource usage.
Long polling is a technique where the client sends a request and the server holds it until new data is available instead of responding immediately. Once data is sent, the client quickly makes another request, enabling near real-time communication.
Example: A messaging application where the client sends a request and waits until a new message arrives; once received, it instantly sends another request to keep listening for further messages.
In this example, we will understand long polling, where we will use AJAX for understanding long polling, although, we can use normal HTTP requests as well. The steps are described below:
data.json:
{
"name":"Mridul",
"age":"24",
"city":"Banglore"
}
Output:
Long polling is ideal for applications that require near real-time updates without empty responses. Common use cases:
These make long polling more efficient for real-time-like applications.
These drawbacks are mostly related to complexity and server resource handling.
Both techniques are used to fetch updated data from the server, but they differ in how frequently requests are made and how the server responds.
Short Polling | Long Polling |
|---|---|
It is based on Timer. So, it is used for those applications that need to update data at a fixed interval of time | It is based on getting the response. So, It is used for those applications that don't want empty responses. |
Here, an empty response can be sent if a response is not available. | Here, empty response is typically avoided but can still be sent in case of timeout |
It is less preferred. | Preferred for near real-time use cases compared to short polling, depending on system requirements. |
It creates lots of traffic. | It also creates traffic but less than short polling. |