oyed
oyed

Reputation: 570

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:

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.

Upvotes: 1

Views: 883

Answers (2)

Layke
Layke

Reputation: 53146

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

Upvotes: 1

Derk Arts
Derk Arts

Reputation: 3460

Have you tried a settimeout in JS that starts over every time PHP returns no updates?

So:

  • Call JS function that checks PHP via Ajax (checkPHP()).
  • PHP sees no new values, returns '0' for instance
  • In the data receive function for the AJAX function, check the return, if return value == 0: settimeout(checkPHP,5000);
  • If return value != 0, do your thing.
  • You could display a waiting icon as long as return value != 0

Upvotes: 0

Related Questions