AJAX, PHP sessions and concurrent requests

I'll just think of it!

My javascript is sending about 20 AJAX requests to my PHP file for response (via external web API) when the user submits their search. The results are stored in an array in an array of sessions.

I read that browsers only allow two simultaneous requests to the server.

My first problem is that although there are still a few requests still waiting for a response, the "add to cart" AJAX request won't work as it is still waiting for the rest of the requests to complete.

My second (and more annoying) problem is that two requests being processed at the same time seem to be overwritten with each other, so when all responses are complete, half of them are in the sessions array. Either all are odd, or even those that depend on whether the last query is odd or even.

I would prefer not to send requests individually (i.e. only send the next one when the last one is finished) as this will slow down the user experience for a bit of an honest bit.

Is there a solution for this dubbing session or should I use a completely different approach altogether?

Thanks everyone!


Edit:
It is for checking domain availability. The user searches for "mydomain", and results in com, net, org, etc. Ultimately presented.

Sending one request and searching the script for all tlds at a time means that no response is returned until all results have been received. The result for some tlds seems to take up to and more than 30 seconds, during which the user does not receive any feedback, except for the swirly icon and "Please Wait" (which is what happens when javascript is not enabled).

Separate queries allow me to display domain availability as it comes in.

I am currently thinking about how to send one request and then use javascript setinterval to re-validate the session until all the results are there.

+1


source to share


8 answers


I think you should start refactoring your solution:



  • All performance recommendations state that you should minimize the number of HTTP requests. 20 is too much
  • If you have a share, you need to lock and unlock the parts you manage to prevent two or more requests from updating at the same time.
+4


source


Requests are processed in parallel, which means parallel programming (with threads) including race conditions etc.



My suggestion was to just send the search action (assuming the user only performed one search, not 20) to the server, and split it into 20 actions you want to perform. This allows you to execute them sequentially without allowing them to overwrite each other.

+1


source


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

Save data from the session in local variables, then call this function to unlock the session for other files. (although it must be said that 20 AJAX calls are probably not the best solution)

+1


source


It's a shame that this is not the answer you want, but 20 queries sounds too much for one search. By introducing something similar, i.e. a short search history saved in the session, we decided not to use AJAX at all. There's a time and place for it, but not if it's going to kill your server with requests when your traffic increases.

0


source


Try making a QUEUE request for your ajax calls. Each call will be completed after the end of the previous one.

After the second request is made, you cannot be sure what will happen, since, as you said, only two simultaneous requests can be sent. After this number, the 3rd query will most likely replace the second, etc.

0


source


I am guessing this is some sort of autocomplete window. If you are using Scriptaulous' Ajax.Autocompleter, you can simply specify the "minChars" parameter. It won't send an AJAX request until at least a lot of characters have been entered.

You can also configure the "frequency" parameter, which changes how often (in seconds) the input field should be polled for changes before triggering the AJAX request.

More details here

0


source


As others have pointed out, your first approach should be to reduce the number of requests. If that's not an option, you can use subdomains to increase the number of concurrent requests.

Configure your DNS (e.g. mydomain.com) to accept all * .mydomain.com subdomains and send them to the same server.

Then you can send different AJAX requests to different domains (a.mydomain.com, b.mydomain.com, ...)

This approach is used by map servers like google maps to increase the number of mappings loaded by the browser in parallel.

0


source


The problem is that between a concurrent request a session starts. even if it is opened in a different context, due to the saved read of the session and the same cookie, the session opened by each request is the same. Then if one of the current requests finishes first its task, it closes the session as always when the script has finished its task, but since another script (another callable) may still be running, it is not that the session has already been closed, so there are some solutions ... Before completing the task, copy the session var to sessionholder var, restart the session, restore the session values ​​session = sessionholder and session_write_close Which session will start or end first? sometimes there is no way to know, maybe the first one that started the session will take longer to finish its task,while the other comes and goes, somehow, takes it as it is, the session is nothing more than storing a variable called session, type an array with the key values, handling restoring the stored values, and then updating the persistence of those values ​​at the end of the script task is what makes the session automatic in this matter, so it makes no difference if you start the session (restore its saved values, assign new values ​​to it, then save them again, the process starts the session, assigns values, closes the session. Of course, have in mind, if you restart the session without its actual values, they will be lost (overwritten if they exist), so you must first save your actual values, restore, reset the current values, and then write.Another process intrinsic to a specific session variable that php does is to assign a link (session id) that it sends to the client (usually via a cookie), so when a cookie is returned with that link id, it identifies the session variable saved for recovery previous values. Remember that the same call can do the same thing, since ajax is asynchronous, if for some reason one call is delayed and users click again, the new request does not end the current one and it will be the same problem, so if you are using ajax prepare to handle sessions as needed depending on your application. Finally, remember that the call can be in a different file or in the same file, one ajax prices call request.php other products.php but both will use the same saved session variable, session var is just one time for all your scripts. Avenida Guez. Thanks for reading

0


source







All Articles