tags:

views:

321

answers:

3

I have a page that already makes multiple AJAX requests at the same time. They use the Comet model, so there are always multiple HTTP connections open, waiting for more data from the server. As the page gets more complex in the future there may be even more. I'm concerned about opening too many connections.

I wonder if there is a way to "multiplex" them, ie. have a single connection through which all requests are sent and route the messages to the appropriate handler functions on both the server and the client sides.

So far I can't figure out a way to do this, because once I make an HTTP request and it begins waiting for data I don't think it's possible to send more request data. I could close that connection before opening another one to send more data, but what if the server sends a response just then?

Am I trying to do something I shouldn't?

+1  A: 

Why do you think that it's impossible to have more than one connection open? Did you try?

An approach we used was to use notionally two connections: one for Comet, all asynch delivery was multiplexed over that, so many different message kinds would arrive there. Reuqest/response happened on the other In effect we just wrote a conventional Ajax app, with Comet as an extra.

djna
It IS possible to have more than one connection and that's what I already have. I'm just concerned about browser limitations if I open, say, 10 connections - and the user has several instances of my page open. But multiplexing only the responses is an interesting idea, since request connections can be very short-lived. Hmm...
Evgeny
A: 

There is a agreement of browser developers - that only 2 simultaneous request can be sent to the same subdomain. That is why multiple AJAX requests are useful, but keep queue in mind. On other hand JScript invocation restricted by security reason to single subdomain. There is small trick to leap over this restriction (see for example http://www.simple-talk.com/dotnet/asp.net/calling-cross-domain-web-services-in-ajax/ section "Dynamic Script Tag hack")

Dewfy
I'm not sure the 2 connections limit holds true anym ore. It's now 6 for IE8 http://msdn.microsoft.com/en-us/library/cc304129(VS.85).aspx (and it's always been possible to change this with a Registry settings for earlier versions of IE.) I don't think Chrome limits itself to 2 either. Not sure about Firefox, Safari or Opera.
Dave Webb
@Dave Webb - it is rather curios if you will ask your Web clients to change settings before AJAX page start. That is why I've told about keeping agreement in mind. "be ready for the worst"
Dewfy
A: 

I ended up doing what djna described (at least I think it's the same thing): have one Comet-style connection always open that keeps listening for responses, but send AJAX requests using new connections, as normal. Each request returns immediately with a "job ID" (so the connection is very short-lived) and any further data for that "job" is sent down the Comet connection. This way there are at most 2 connections at any given time, unless 2 AJAX requests happen at the same time, which is unlikely in my case.

Of course, there may be multiple clients using the page at once, so to figure out which job responses need to be sent down the comet connection I also have a "comet session ID". This ID is generated when the page loads and doesn't change for the lifetime of the page. After the normal AJAX request that creates a job I send another, very simple AJAX request with the job ID and session ID. This associates that job with the session and tells the server to send all responses for that job to the alread-established connection associated with that session ID.

I could have made the session ID a part of the initial request, of course, but this would mean that each AJAX method on the server-side would need to know about it and I'd rather keep it separate, as part of the infrastructure.

Evgeny