Tony
Tony

Reputation: 12695

jQuery check server updates in a setInterval function - what else can be used?

I want to create the code which will check if e.g some new muser messages appeared and show its count. I know it's simple - just make an ajax calls in the setInterval function:

$(document).ready(function () {

    setInterval(function () {
        //create an ajax call
    }, 30000);

});

but it's not the point - I can't find any information of the other approaches. Is it the only way to achieve this by set the setInterval function to check the updates ?

Edit

Ok, I've found http://socket.io/ maybe it's what I'm looking for

Upvotes: 4

Views: 566

Answers (2)

dkellner
dkellner

Reputation: 9926

You could as well use setTimeout on callback, so that every time something comes back from the server it will set another timer for like 30 sec later. There are advantages of this (server won't be overloaded with your requests even if the response is slow) but also drawbacks (once there's no answer poll won't run again).

Also there are other ways of keeping an eye on the server; search for the term "long polling" in Google.

Upvotes: 4

Bas van Dijk
Bas van Dijk

Reputation: 10713

This is the easiest solution. Otherwise you should use sockets like http://socket.io which is more low level but in this way you could implement push messages to the client.

Here is a tutorial of a nodejs and socket.io push notification server: http://www.gianlucaguarini.com/blog/nodejs-and-a-simple-push-notification-server/

And this is another one for PHP: http://pusher.com/tutorials/html5_realtime_push_notifications

Upvotes: 2

Related Questions