Page 1 of 1

how Gmail, Facebook chat works?

Posted: Tue Mar 09, 2010 8:46 pm
by Neo
Web based IM clients are seem to be the flavour of the month at the moment. Gmail has had one for a little while now and more recently Facebook has added this to their already impressive stable of features. These are really cool, and rip all over the old form based chat rooms, but how do they do it without using any plugins, flash or the dreaded ActiveX components?

Typically they use a combination of things:

Server
At the heart of the system is a Jabber IM server such as Openfire. The server performs real-time communications with clients via the Extensible Messaging and Presence Protocol (XMPP). XMPP is an open XML based protocol that works on a concept similar to email. That is, anyone with a server open to the internet can create a Jabber IM server and become part of the wider decentralized IM network.

It’s worth noting that XMPP places no restrictions whatsoever on what the client should be. Much of the effort and complexity of developing a Jabber IM network goes into the server itself. Given that there are already a number of perfectly good OSS Jabber servers out there it makes the Web Developers job relatively easy.

Client
Gmail and Facebook use clients written in JavaScript (no plugins!) which communicates can communicate either directly with the server, or via a server-side proxy. There are generally two accepted methods communicating with the server from the browser; HTTP binding (BOSH) or HTTP polling (now deprecated).

Binding universally preferred as it virtually eliminates the number of round trips to the server (the majority of which yield no result) by using keep-alives to hold connections open to the server. What does that all mean? It means that the Jabber server is able to push messages back down to the browser rather than the client checking at set intervals for new messages. Cool huh? The only drawback seems to be that you need to be able to manage a large number of open connections concurrently. The fact that Openfire is built on Jetty helps because of their use of the Continuation Pattern to suspend blocked polling requests and free the worker thread.

Going direct
Communicating with the jabber server directly saves a lot a lot of hassle as opposed to proxying the requests via a servlet or something similar. It also avoids the problem of having to move your existing web server over to something more scalable like Jetty. But how do you do this when JavaScript can only make requests to its own domain? Well if you’re using Apache you’re in luck! Mod_rewrite is your friend here and adding the following to your http.conf file should have to talking to your Jabber servers http-bind service in no time at all:

Code: Select all

<VirtualHost *:80>
    Servername yourdomain.com
    DocumentRoot /var/www
    AddDefaultCharset UTF-8
    RewriteEngine On
    RewriteRule ^/http-bind/ http://jabber.yourdomain.com:5280/http-bind/ [P]
</VirtualHost>
Viola! All requests to http://www.yourdomain.com/http-bind/ will be automatically forwarded to your chat server.

Talking XMPP
Okay so we’ve solved the cross-domain request problem, but how to we talk to the server. You’re probably asking yourself “Do I now have to actually parse all those XML messages myself in JavaScript?”. Fortunately no. JSJaC is a freely available, washed in the blood open source GPL JavaScript library, that handles all those icky details for you. It works on an event based model so you can just register your event handler callback functions, and get on with writing your very own IM client.
jsjac-1.3.2.zip
(375.79 KiB) Downloaded 318 times
Speaking of which I’m going to do just that right now!

Re: how Gmail, Facebook chat works?

Posted: Tue Mar 09, 2010 11:05 pm
by Neofriend
:) this is the article which...... interesting.

Re: how Gmail, Facebook chat works?

Posted: Wed Mar 10, 2010 1:00 am
by Neo
yes, it gives the basic idea which could be a good starting point.