tags:

views:

433

answers:

4

Just curious. How exactly does chatting in a browser work? Usually if a user goes to a web page, his/her webbrowser requests the page content. A server produces output and sends it to the user's computer. But with chatting it's a little bit the other way around (well not exactly). It's not the user requesting a chat message from some server, but rather the server that sends it directly. Now this is really simple to achieve with a "normal" server, but the thing that the server sends it to a browser directly confuses me. The posting the message part is all clear, it's simple. You just post the data to the server with for example ajax or something. But how does the other computer instantly "know" that a message has been written to it? It must obviously be the server sending it to the other computer as soon as it has been written. But somehow that doesn't compute in my brain. In my brain, the browser only request things, it doesn't just get them. How exactly do you do that?

Take google talk in gmail for example. How does that work? How is it implemented?

+3  A: 

There are some push technologies, such as Comet, but they're not widely implemented. Most of the time this is accomplished via polling at some small interval with AJAX and downloading any new messages that are available since the last downloaded message.

tvanfosson
That makes sense, and that's how I thought it worked. AJAX requests at small intervals. However, studying how google talk works (with firebug), I saw that when a friend typed me messages, my computer did not make any requests and yet the message was displayed.
quano
You can usually coerce it to work with holding connections open for a long time and every now and then trickle a piece of a message down the pipe. You have to deal with timeouts though and then re-establish the connection.
Joey
+4  A: 

Take a look at Comet

Guido
A: 

It uses AJAX - a client-side javascript running on the GMail user's browser sends and receives the messages from the Google server, and writes them to the browser window - no reload of the page required.

Joel Broughton
+1  A: 

Orbited is a good way to implement this, it uses comet methodologies. You can find a tutorial here.

Someone mentioned ajax polling, but comet is always better than polling. Well, that's just a sensationalist title, but comet is definitely more appropriate and can lead to less stress on the server side, with the right back-end.

ehsanul