views:

36

answers:

3

Hi,

How the ilusion of causing site to change content from server side is done? Let the example be gmail chat or chat on facebook. Or even new message sign on stack overflow.

Is it done by http://en.wikipedia.org/wiki/Comet_(programming) ?

Thanks for help

+4  A: 

That sort of things is usually done with a block of JavaScript firing again and again according to a timer. It will check the state of the things in the database and adjust something in the markup. For instance, change the CSS class of some element to introduce a different color or a bold font, replace a picture with the one done in a brighter color etc. Quite simple really. No magic involved.

Developer Art
Thats all ?Isnt it sort of active waiting ? I dont like that idea :/
gruber
Yes, that's it. Nothing more to it. Try disabling JavaScript for any site with this feature you have in mind, and you'll see their notifications stop working.
Developer Art
@snorlaks: there's no other way around javascript data polling to answer your question, too bad if you don't like the idea!
Marco Demajo
+1 for figuring out what he was asking!
egrunin
@snorlaks: Your "asynchronous" requirement means javascript/JQuery. Look up AJAX and any other web-based asynchronous techniques, it's all javascript.
Dave Swersky
I knew there must be any other way:http://en.wikipedia.org/wiki/Comet_(programming)
gruber
A: 

The client side has to 'poll' the server for changes. i.e. a timer based Ajax call that checks the server every 15 seconds for new data, and takes action based on the result.

very loose example:

setTimeout('checkMessages()',15000);

function checkMessages() {
    //using jquery
    $.get( .......... , function (data) { if (data == "newmsg") { $('#newmsgind').blink(); });
    setTimeout('checkMessages()',15000);
}

Web browsers don't really maintain a connection to the server. You pull a page and that's it. Ajax allows continuous asynchronous communication, but it's always the client that initiates.

Fosco
So in your opinion for example google chat on gmail is made like that ?
gruber
I'm sure the implementation is far more robust and evolved, but yes.
Fosco
its not true that client side hat to poll:look at this: http://en.wikipedia.org/wiki/Comet_(programming)
gruber
@snorlaks, if you read the article you'll see it always involves polling by the client side.
Fosco
A: 

If you really don't like the javascript approach, you can write a Java applet that works the way you seem to prefer, maintaining an open connection to the server. But that's a heavyweight solution to what is usually a lightweight problem.

egrunin