April 24, 2008

Facebook Chat

Today Facebook released chat that looks similar to Gmail. I have written an article about how chat works in Gmail. Facebook chat is also based on HTTP but they are not using any hacks like Gmail chat. Here is how it works.

Check it out my previous article on why chat in HTTP is challenging. Facebook's approach for this straight forward. They always opens one connection to Facebook server and when ever you receive a chat message from your friend they show it in chat window or in case of timeout they open one more connection immediately and waits for response.

Client A -----> Server -----> Client B

Fig 1.

For example, suppose A sends a message to B. Since A can't direct sent the message to B it has to post that message to Facebook Server and which inturn should send the same to B. Here Both clients A and B opens a connection (This is an Ajax request) to Server and waits for Server response. Server will respond only if it has any outstanding message pending to that client. (In our example message from A to B). Since there is already a connection B opens to server it will immediately get the message A had sent. For instance if there is no message, the opened connection will be timed out (this is 300sec for Firefox. You can change the default value by changing network.http.keep-alive.timeout ) and Facebook again opens a new connection (Ajax request) and above process repeats.

The URL that Facebook opens to Server will looks in follaowing format.

http://0.channel10.facebook.com/x//true/p_=803&

Check it out my previous article. How Gmail Works !.

1 comment:

  1. This is the model I was referring to when you asked me about Gmail chat model the other day.

    Which model do you think is better? I would think Gmail chat's is but for the fact that it is more involved than Facebook's. Also if there are some non HTTP 1.1 complaint proxies along the path, that would cause a problem for Gmail chat.

    ReplyDelete