Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple simultaneous running ajax requests in ExtJS 4

Tags:

php

extjs4

Problem

I have a long running import job which I start with an ajax request, it could take some minutes until the request is finished. While this first ajax request is running, I want to have a look at the server to know how far the import is gone, this second request will be done every 2 seconds or so.

When I use the Ext.Ajax method the requests seems to be chained - the first ajax request (import) runs until it is finished, just then the second (import update) is fired.

I saw that Ext.Ajax is singleton, so maybe thats the reason. So I tried to create my own Connection objects with Ext.create('Ext.data.Connection') but it doesn't work.

My current request chain is:

  • first request - start
  • first request - end
  • second request - start
  • second request - end

But it should be:

  • first request - start
  • second request - start
  • second request - end
  • ...maybe more second requests
  • first request - end

Question

The browser should be able to handle multiple request, there must be a limitation inside ExtJS but I didn't find it?


Update 2011-10-16

Answer

The problem wasn't ExtJS - sorry! It was PHP, my first script works with the session and the second script tried to access the session as well. And because PHP sessions are file based, the session file was locked from the first request script and the second request script had to wait until the first release the session lock.

I solved this with this little piece of code I added to my import process (the first script) after every x row:

 $id = session_id();
 session_write_close();
 sleep(1);
 session_start($id);

So it stops and reloads the session and the other script was able to hook in and get the session information.

like image 799
Marc Avatar asked Oct 26 '11 05:10

Marc


People also ask

How do I send multiple Ajax requests at once?

If you had two server side page for your two AJAX files then it would be fine. Try to create two different aspx file for you different methods to have them called together.

Can we execute run multiple Ajax request simultaneously in JQuery?

There is a requirement to make multiple AJAX calls parallelly to fetch the required data and each successive call depends on the data fetched in its prior call. Since AJAX is asynchronous, one cannot control the order of the calls to be executed.

How do I stop multiple Ajax calls from repeated clicks?

for prevent multiple ajax request in whole site. For example: If use ajax request in other ajax page, Using ajax in php loop, etc, Give you multiple ajax request with one result. I have solution: $(document).

What happens when one Ajax call is still running and you send an another Ajax call before the data of first AJAX call comes back?

Since Ajax calls are asynchronous, the application will not 'pause' until an ajax call is complete, and simply start the next call immediately. JQuery offers a handler that is called when the call is successful, and another one if an error occurs during the call.


1 Answers

Singleton or non-singleton doesn't even change the way Ext.Ajax works. I think this could be due to the coding (did you wait for the calls to finish?)

Afaik, I never have this problem before when I do multiple calls. The only thing that is hogging the calls is the server (PHP), which doesn't support parallel processing and causes delays, and generate a pattern like this

  • Call 1 - start
  • Call 2 - start
  • Call 1 get processed in the server and Call 2 get queued up
  • Call 1 - finished
  • Call 2 get processed in server
  • Call 2 - finished

It could be disastrous if Call 1 requires more time to process than Call 2.

EDIT:

I have written this little demo just for you to feel how does it works. Check it out :) Spent me half an hour lol!

like image 165
Lionel Chan Avatar answered Nov 06 '22 07:11

Lionel Chan