Long Polling: A Quick and Simple Rundown

Google+ Pinterest LinkedIn Tumblr

Chat APIs, Audio APIs, and Video APIs can be built in many unique ways – especially with the number of contemporary implementation styles and types of backend server technology. However, in your research to evaluate the fundamentals of some of these API technologies, you may come across some of these terminologies that may seem unfamiliar to you. This series of posts will teach you everything you need to know in order to properly evaluate your options when considering APIs to support your applications. This article will explore the concept of long polling and give you a well-rounded understanding of its benefits, drawbacks, and implementation styles.

What is Long Polling? 

Long polling is the simplest way of maintaining a persistent connection with a server, especially one that doesn’t use any specific protocol like WebSocket or Server Side Events (SSE). It essentially holds the request until it has a response to send back. If it doesn’t have a response, the connection will simply hang until something gets sent back.

It is more effective than the regular polling because it reduces the number of requests and responses that need to be sent back and forth. Repeated requests and responses can get computationally expensive very quickly, especially when scaling up the servers to provide for more users. 

Long Polling Structure

Long polling, more often than not, follows something similar to the following outline.

  1. A request is sent to the server.
  2. The server doesn’t close the connection until it has a message to send.
  3. When a message appears, the server responds to the request with it.
  4. The browser makes a new request immediately.

You will notice how the browser doesn’t repeatedly have to send queries to find the status of its request. This is one of the many benefits of long polling.

Short Polling vs Long Polling: Benefits + Drawbacks

Short polling is a simpler yet significantly more inefficient method of request management. Short polling is a timer-based strategy that calls at fixed delays – for example, every 5 seconds – whether the request has been processed or not. This would mean that the client would have a consistent stream of responses coming from the server, but it would also mean that it would take a minimum of 5 seconds for a response. Regardless of how quickly the server processes the request, the client would be forced to wait until the timer has completed, which causes inevitable and avoidable latency. Long polling avoids this latency by responding as soon as new data is available. This makes it greatly effective for situations involving variable-rate changing data. A perfect example would be something like a Chat API because messages can be sent at variable time intervals throughout the day.

Short polling will also, in most cases, end up sending a greater number of responses in comparison to long polling. This is because it is repeatedly sending responses regardless of the status of the request. Long polling would allow the clients to get one response for every request, just at a different time interval. 

However, it is also worth noting that Long polling can increase overall server load because of the number of connections it would have to maintain. Short polling, from that perspective, is much more efficient because it closes connections once responses are sent. Another issue is that a server may send a response, but the browser issues may prevent the message from being successfully received. Unless there is some way to verify if a message has been received or not, a subsequent call to the server may result in multiple missed messages.

Taking information of these sorts into consideration allows developers and engineers to make an informed decision regarding what technology they should implement into their backend servers.

Applications of Long Polling

Long polling works best when it is implemented in a scenario where the updates to the data is rare, as that is when the benefits of non-repeated responses become greatly significant.

One of the many applications for long polling include chat-based applications. As messages are only sent every once in a while, it can be rather inefficient to use short polling for such situations where new data is not always guaranteed. Therefore, sending requests when a message is sent is much more effective in minimizing unnecessary responses being sent back and forth. Another application could possibly be turn-based gaming. A request can be sent when every turn is complete, instead of relying on repeated requests. Similar to chat applications, it would also only need the server to be notified when a turn is complete. Web applications with low-frequency data can also be developed in a way that concepts of long-polling can be applied to them. They can query the server only when a user wishes to travel to a different tab, as opposed to constantly checking what page the user is on and to see if the user has clicked on a new page yet.

How is it different from real-time APIs? 

Long polling is a method in which real-time data transfer and communication can be achieved. It can be compared to short polling, Websockets, and other forms of server data communication. There are also many circumstances where multiple of them would be used in conjunction with each other in order to achieve the most efficient request management system. Long polling is often used in Real-Time Chat APIs as it is more effective in reducing latency between the sent request and sending responses to users when a message arrives.

Applozic’s Implementation:

Applozic’s API allows you to seamlessly integrate all of your Chat and Audio applications with the amazing and powerful SDK created by our developers. Using an SDK like ours would allow you to not even break a sweat about what specific technologies should be used for your SDK. Applozic’s well written documentation will allow you to implement an in-app chat or in-app audio channels with ease and minimal hassle. If you want to try and implement it for your own, there is a free trial available for all of your testing purposes so you can sign up today.