In August 2005, Google launched an instant messenger: Google Talk. It was similar to Yahoo! Messenger or AOL Instant Messenger in that you had to download and install a little program to be able to send and receive instant messages. A few weeks ago, Google Talk was integrated with Gmail, so Gmail users can send instant messages to each other directly from the Gmail web-based interface.
While web-based chat solutions have been available for a long time, by integrating Google Talk into Gmail, it instantly became the most widely used web-based instant messenger. Getting a web-based instant messenger to work smoothly for such a large number of users is no small feat.
But even more interestingly, unlike most Ajax applications, for Google Talk to work as a real instant messenger, the server needs to be able to notify the browser when a new message arrives. With email, polling can be used: as Gmail is loaded in the browser, some JavaScript code can send a request to the server every minute or so to check if a new message has arrived. That wouldn't work for instant messaging: having to wait 1 minute or even 10 seconds to receive a message wouldn't be instant anymore.
While the XMLHTTPRequest object works perfectly to request data from the server, being notified by the server requires the client to open a connection with the server, keep it open indefinitely, and have the server send notification to the client over this connection. As the XMLHTTPRequest object cannot be used for this purpose, people have been using an invisible iframe to establish the connection with the server. The idea is that the iframe loads an "infinitely long" page which contains JavaScript. The page is "rendered" as data is received by the browser, so when the server wants to send a notification to the client, it sends some JavaScript over that connection, and the JavaScript is instantly executed by the browser.
However, this method has a significant downside: while the document is loading in the iframe, on IE the logo on the top right of the window keeps spinning. This gives a wrong impression to the user. The clever people at Google found a solution to this IE-specific problem: instead of using an invisible iframe directly in the page, they create an instance of the "htmlfile" ActiveX object, and create the iframe in that object. Alex Russell discovered this clever hack in the Gmail code, and I totally agree with him on this:
From long and painful experience and amazingly deep respect, I take my hat off and bow to whoever it was on the GMail/GTalk team that figured this out. It’s a hell of a hack. It’s no wonder that Google has been able to attract and develop the best DHTML hackers in the world.
No comments:
Post a Comment