What is Long Polling?

Asked

Viewed 5,948 times

15

Whenever I search for "periodic updates" and "real-time notifications" for web applications, I come across the term long Polling.

From what I understand, it seems to refer to periodic updates made with AJAX, for example, with a setInterval javascript,

But I’d like to know more deeply:

  • What is long Polling?

  • Use long Polling, for the purpose of keeping an application up-to-date, it expends a lot of server resource?

  • The term long Polling applies, in programming, only in the context of Web, AJAX, Javascript and the like?

  • 1

    Comet has nothing to do with long Polling? If yes, related: https://answall.com/q/32563/64969

  • Look, I don’t know if it will help a lot, but I found a very informative site about Long Polling, in case I wanted to take a look, I think you’ll find what you’re looking for: imasters.com.br/article/23436/javascript/see-how-how-long-Polling-can-help-you-develop-applications-in-real-time/? trace=1519021197&source=single

  • Only to associate the question with another that has an example https://answall.com/questions/64080/qual-a-difference%C3%A7a-entre-long-Polling-e-o-normal-ajax

1 answer

24


Long Polling is a technique that simulates server downtime to keep an HTTP connection open. This technique was created from the need for real-time communication with a server web.

Context

With the need to get real-time data from the server, developers used to create routines that sent a request from time to time (as you yourself described) to "ask" if there were changes in the application’s data model. If there were, the request reply would contain this data and then it could be updated in the application.

This type of routine consumed a large amount of resources when the application tended to be scalable (to grow rapidly). Let’s simulate:

Imagine a page that sends an AJAX request to an every 5 seconds endpoint specific to find out if there have been changes to notifications, for example. This does not become a problem with a few users simultaneously connected. But let’s say the application grows from 10 users to 100 connected users simultaneously.

Let’s think bigger. Imagine 1,000 connected users. In 5 seconds, 1,000 requests will be sent to the same endpoint. After 5 more seconds, 1,000 more and so on. Depending on the application architecture, this starts to overload the server. Specifically that endpoint.

"The solution" - Long Polling

When a client requests the server simulates an unavailability of data and makes the HTTP request unanswered until there are changes to the application data model.

Does that solve the problem? Yes and no. Yes, why it is not necessary to keep sending requests for times and times to check these changes. No, because this caused another performance issue. HTTP requests were not specified for this. Extending an HTTP request may "seem" to solve the problem of the amount of request sent to it endpoint, but in fact it also consumes a lot of resource precisely because it is being used improperly.

The real solution - Websockets

I wrote a post about Websockets and its implementation in Java (but it has implementations for almost all current languages). I suggest you take a look why there I explain in detail about the specification and also give practical examples that can be reproduced in your home.

Real-time communication with Websockets

Anyway. Focus your study on Websockets implementations and be happy with real-time communication.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.