Reputation: 61971
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?
Upvotes: 3
Views: 2105
Reputation: 55907
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 async delivery was multiplexed over that, so many different message kinds would arrive there. Request/response happened on the other In effect we just wrote a conventional Ajax app, with Comet as an extra.
Upvotes: 1
Reputation: 61971
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.
Upvotes: 0
Reputation: 23624
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")
Upvotes: 0