Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pros and cons of long polling vs. regular polling?

I have a web page that I need to check the server for an update every second. Updates could be relatively frequent or infrequent. There could be a lot of web clients simultaneously checking the server for updates. This could either be an AJAX request every second, or a "long poll" which emulates server push. Which method would I want to use and why? It seems like the overhead of initiating an HTTP connection every second might make the long polling method preferable. On the other hand, there is probably a limit to the number of concurrent connections that can be maintained by the server. Some comparisons of these techniques would be useful for me to decide which way to go.

like image 844
Nate Reed Avatar asked Dec 04 '10 17:12

Nate Reed


1 Answers

It depends on your webserver. There are newer webservers being developed that understand AJAX/Comet style and make long-polling very efficient. See Mongrel2 for an example.

You might also consider regular polling, but making it dynamic. Not knowing your domain, I can't make a specific recommendation. But imagine in a chat application. Instead of polling every second, I might wait a little after a chat message is sent to give the other person time. Then check a little more frequently for a while, and if I still get nothing, slow down (as the other person might be gone). I might have poll intervals from 1 second to 30 seconds depending on the situation.

It would all need to be tested for feel, but on average, I might be able to make it feel like 1 second polling, when on average it's more like 20.

like image 61
Lou Franco Avatar answered Sep 22 '22 15:09

Lou Franco