Update (2017): This post is outdated and shite. Please look elsewhere for better examples of similar apps. If you insist on seeing the naive code I wrote for the demo, there is a github link in the post.
(Open the chat in two or more browser windows/computers and check how fast the realtime updation is!)
Gmail chat, Facebook chat etc were implemented using a technique – rather a hack – called long polling. It is the technique where the client initiates a request to the server. The server rather than returning the client with a response, keeps the connection open for a longer time. The server only returns to the client with a response when it (server) has something new for the client. The advantage of long polling over normal polling are reduced latency and saving from the overhead of sending too many short http requests.
Still, long polling is the same old request-response http transfer mechanism. It wasn’t created for realtime communication.
Then came WebSockets. WebSocket is a technology that enables full duplex communication between two nodes. At the beginning of the communication, the client makes a handshake with the server and opens a connection with the server. Once the socket connection is opened between the server and the client, both the server and client can send data to each other freely without further solicitation. Doesn’t it sound awesome?
The WebSocket spec is still in draft state as of writing this post, but most of the browser vendors have already started adding support for websockets.
In the demo, WebSocket is used for opening socket communication with the server.
And NodeJS with websocket module is installed to support evented communication and websocket protocol support in the server.
PS: The above demo is running in my test server. So I might be down at times when I’m frying the server 😉