I'm using AJAX to implement long polling on a page which I want to update whenever new rows are inserted into the database. The way this is done is with jQuery's .ajax()
calling, for example, poll.php?ky=41
, which queries the database a number of times for rows with an ID of > 41
(with a small wait in between) until a specified timeout. It will fulfil the request immediately if there is a result, or after the given timeout.
In effect, this keeps a hidden connection always open to the server, waiting for it to respond, in order to get notifications.
This works, but I'm using DreamHost and after 8 php53.cgi
worker processes are spawned, no requests to my site are fulfilled (i.e. forever loading) until one times out. This affects every other page on my website. The HTTP Server is Apache 2.2.22-14.
To alleviate the problem, I have reduced the delays and the timeouts so it is closer to regular polling, and added longer delays when there have been no updates for a while. This means notifications may come a few seconds late, but so far my server has been running fine.
What I'm worried about is how well (or rather, how poorly) this will scale.
My question is: given that I'm on a shared host (DreamHost), and this page must be compatible with as many browsers as possible (except mobile), is there a more efficient way to get instant "push" notifications from the server?
Or, what other options do I have? Should I switch back to regular polling?
TL; DR
Polling is fast, but long-polling (waiting before fulfilling the AJAX request) ties up resources. The difference is that long-polling will get the result as soon as it arrives, whereas polling will only pick it up the next time an AJAX request is sent after the new information comes in. Ideally, I would like the advantage of long-polling without tying up threads & causing other users to wait before pages are served.