tags:

views:

1482

answers:

6

I'll get straight to the point!

My javascript sends about 20 AJAX requests to my PHP file to respond to (via an external web API) when the user submits their search. The results are stored in an array in the session array.

I've read that browsers will only allow 2 simultaneous requests to a server.

My first problem is that while there are still more than one requests still waiting for a response the AJAX "add to basket" request won't work since it's still waiting for the other requests to complete.

My second (and more annoying) problem is that the 2 requests that are being handled simultaneously seem to be over writing each other so that when all the responses are complete only half are in the session array. Either all the odd ones or even ones depending on whether the final request is odd or even.

I'd prefer not to have to send requests individually (ie only send the next when the last has finished) as that would slow things down for the user a fair bit.

Is there a solution to this session overwriting or should I be using a completely different approach altogether?

Thanks all!


Edit:
It's for checking domain availability. The user searches for "mydomain" and results for com, net, org, etc are eventually presented.

Sending a single request and having the script search for all tlds in one go means that a response isn't returned until all results are in. The result for some tlds seem to take upto and over 30 seconds during which the user is getting no feedback save for a swirly icon and "Please Wait" (this is what happens when javascript isn't enabled).

Seperate requests allow me to display the domains availability as they come in.

I'm currently thinking along the lines of sending a single request and then using javascript's setinterval to repeatedly check the session until all results are in.

+1  A: 

Requests are processed in parallel, which means that's similar to concurrent programming (with threads), including race conditions etc.

My suggestion would be to just send the search action (assuming the user performs only one search, and not 20) to the server, and split it there over the 20 actions that you want it to perform. That allows you to execute them in sequence, preventing them from overwriting each other.

Linor
A: 

Sorry it's not really the answer you're after but 20 requests sounds like far too much for a single search. Having implemented something similar ie. a brief search history stored in the session we opted not to use AJAX at all. There's a time and a place for it but not if it's going to kill your server with requests when your traffic increases.

sanchothefat
+3  A: 

I think you should start refactoring your solution:

  1. All the performance guidelines states that you should minimize the number of HTTP requests. 20 is too much
  2. If you have a shared resource you need to lock an unlock the parts you manipulate it to prevent that two or more requests update it at the same time
Daniel Silveira
Is sending so many AJAX request any different to, say, a gallery page which has to load numerous images?
Nick
It's VERY different from a performance standpoint. The amount of processing that your webserver has to do to answer requests for, and return the contents of, 20 static files (images or whatever) is absolutely minimal in comparison to what it must do to answer 20 discrete AJAX requests.The amount of processing that the browser has to do for static files is also comparatively minimal.There is a superficial similarity in that the number of HTTP requests issued from the client is the same in both cases, but really - that number is not what you should be basing your decisions on here.
ithcy
It would be far more economical to issue one AJAX request, let your back end do whatever number of searches it needs, return the result set as JSON or some such, and deal with it in the browser. Imagine 500 users simultaneously using your current system - that's 10000 requests in a very short span of time. Your performance will become exponentially worse as you scale up.
ithcy
A: 

Try Building a Request QUEUE for your ajax calls. Every call will be made after the previous one ends.

After the 2nd request is made you cannot be sure of what will happen since as you said only 2 simultaneous requests can be send. After that number the 3rd Request will most likely replace the 2nd, etc.

A: 

http://php.net/manual/en/function.session-write-close.php

Store the data from your session in local variables, then call this function to unlock your session for other files. (though it has to be said that 20 AJAX calls probably is not the best solution)

I.devries
A: 

I assume this is some sort of auto-complete search box. If you are using Scriptaulous' Ajax.Autocompleter, you can simply specify a "minChars" parameter. It won't send the AJAX request until at least that many characters have been typed in.

You could also adjust the 'frequency' parameter, which changes how frequently (in seconds) the input field should be polled for changes before firing an AJAX request.

More details here

lo_fye