Search code examples
phpjquerylong-polling

jQuery Instant Notifications


I am creating a web application, and I am in need of some way to do the following in jQuery and PHP:

  • jQuery sends a request to a PHP page
  • PHP page checks if there is anything new, if not, stalls the jQuery request until there is
  • jQuery gets the response and sends another request, repeating the process

I have tried Long-polling methods on Google with jQuery but they pretty much do the same. This is the sort of thing I'm looking for.

Any help is appreciated.

Thanks.


Solution

  • Just as a precursor, if you do go down that route, you should also make sure that your sessions are not locked per request. You could easily hit a situation when a session gets locked when you make the request.

    As a better option, you might want to just work on a "heartbeat" like solution, by requesting the PHP page every 30 seconds. (which is similar to what StackOverflow) do hear to notify you when a new message has come in.

    Even better than that, would be to use WebSockets in node.js. You could even have PHP notify node.js when something "new" comes in, like you say, so that it is always accurate, and you don't have some PHP script constantly running.

    WebSockets via socket.io