Chapter 5. Request Management

Ajax applications, while powerful and user-friendly, do have some issues relating to the requests sent from the client to the server and the responses received back. A major concern is the number of times that communication occurs between the client and server. If the requests are initiated frequently, the server can get bogged down trying to handle requests from multiple users. Further, the client can become unresponsive while waiting for a large number of responses from the server.

Central to this problem is part of the HTTP 1.1 specification that states a client can have no more than two simultaneous connections to a single domain name at a time. While there are ways of working around this strict limitation, such as using subdomains to handle some requests, most browsers do have a maximum number of connections that can be open at a single time. When using XHR, this limitation is handled behind the scenes: you simply initiate requests as you see fit, and the browser queues them up for sending when there's an open connection. This works fine when requests are few and far between, but when requests are being sent from various parts of an application at different times, the built-in queuing mechanism just doesn't provide enough control over when requests are sent and what requests should be sent first. Fortunately, it's not too difficult to implement a custom request manager that can handle more complex communication patterns.

Get Professional Ajax, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.