Thursday, April 29, 2010

COMET for Chirp User Streams

First there was AJAX and to give it company there was COMET. Incidentally both names of popular disinfectants in North America. In JavaScript AJAX allows the client to PULL information from the server and COMET is the technology where the server PUSHES down information to the client.

I got interested in COMET after going through Chirp User Streams. Thinking it would be a trivial task to call a XHR and keep listening to it. In my mind I kept thinking that it would be stuck on readyState 3 and it will never get to 4. Since it would be stuck at 3 I could use a timer and after every few seconds keep checking for the change in length of xhr.responseText.length. Technically it should work like that.. BUT.. the way IE has implemented XHR it is not possible to check the responseText till it reaches 4. Which means that the request has timed out or ended. That somehow makes no sense at all.

I figured out it was time to investigate and see how Facebook and GTalk were able to work on IE if it does not allow you to read text till it reaches readyState 4. Facebook makes a XHR and keeps waiting, after a minute if there is no data it sends some data and the connection is closed and a new connection is made. Incase there was some event in between that minute the XHR reaches readyState 4 and a new connection is made. In a way facebook makes a new connection for every new event. Here a bunch of events can also be merged together to make it more efficient.

While looking into it I also figured out the other way to use COMET. But in that case you need to make changes on the server side. You basically use an IFRAME and send out information in chunks. Every time the server wants to PUSH some data or call an event it makes a new SCRIPT element with the data. Since the script is executed instantly the client can listen to events that way.

In all this confusion I realized that there is no easy way to use Chirp User Streams using XHR in IE. Might have to look into WININET to get the work done.

No comments: