Today is a good day to code

Using xmlhttprequest in a Synchronous Manner

Posted: December 31st, 1969 | Author: | Filed under: Uncategorized | No Comments »

Using xmlhttprequest in a Synchronous Manner

Picture of IrvinRecently I have, in my work, come across a number of instances where the javascript xmlhttprequest object is being used in a more synchronous manner. That makes sense for a couple of reasons. If you are using Flash and you don't want Internet Explorer to hang up when you download a huge swf and you have AJAX going on, or if you want to minimize your applications usage of bandwidth. IMHO, however, if you are writing AJAX, it makes more sense to make it asychronous.

What I mean by using xmlhttprequest in a synchronous manner is that some coders write a stack to use it. This stack then takes requests and queues them up. There is only a single instance of the xmlhttprequest object taking action at a time. If only a single request can happen at a time, while not being tied to the response, then the action is similar to a serial system. If you are limiting the system to one request, then you almost have to have it limited to one response since the next request probably won't go out until after the first one is finished. Or if a stack writer is really clever, it will only wait until after the first is completed. This still isn't truly asychronous since there is a dependency upon making a request.

My method is to create an xmlhttprequest class and instantiate it upon the need for a request. The system will fire off that request. If another request happens to be necessary, the system will instantiate another xmlhttprequest object and fire off that request. This is truly asychronous. If you want to find that object, I have put it in a blog for download. You can just use the search page and look for xmlhttprequest.

The real downside to this is that IE will only allow two connections at a time, so in IE wininet.dll will limit the number of connections and queue requests. This makes IE slower for AJAX than Firefox, where it defaults to a setting beyond the http 1.0 spec, and / or you can improve the system yourself to allow for more connections. Either way, building truly asynchronous applications is a better way most of the time IMHO.