how to have ajax call simultaneously? - php

A few times a day, when our website is busy, we have more than 1000 requests per second on our database.
During these busy times, when a user clicks on an element and makes an AJAX call and then clicks another element and makes another AJAX call, the second call will wait for the response of the first call.
How can I have this AJAX calls run simultaneously? Is this time space between two calls because of the server being busy? If yes, how can we handle simultaneous AJAX calls?

I had similar problem as you do in the past. Then it was open session on server side. Even if ajax call was async, then it have to wait for server because of lock on session file.
Try to close session write when you do not write anything and then, check you ajax again.
Here you have reference to proper method: session_write_close

Related

Continue AJAX request after page refresh

I have a simple question, but can't find a quick answer. Maybe somebody here can help me.
If I trigger an AJAX request on my HTML page with jQuery and then trigger a refresh while the AJAX request isn't finished yet, does the AJAX request stop? Or does it continue on the server?
Thanks.
The ajax request will stop. Whether or not your server processes the request will depend on whether or not the server completely received the request before it was stopped.
This is a case where it is ok to use async: false to get the behaviour you want. However, it would be better to just wait to do the refresh until the ajax is complete.
I believe it depends on whether or not a session was started on the ajax page.
If yes then the php will 'block' until the ajax request was completed and the page refresh will not occur until such time.
You can however prevent this 'blocking behavior' with session_write_close(); which will leave the session open for reading but not writing
it will be aborted immediately from client side, but it could make changes to database if it having some update operations

PHP requests processing in serial not parallel

I wrote a page (cron.php) that uses the imap library to connect to a mailbox, parse messages, and store them in a database, then echos the results in json. I have a few dozen mailboxes that I need to run this same process for, and so I put together a page (mailboxes.php) that lists all these accounts, each with a button that when clicked, essentially hits cron.php via AJAX and parses the json response to update the page when the process is completed.
I've noticed however that if I click each of these boxes, they return as if running serially, not in parallel. Is there a configuration option someplace that might explain this?
Yeah you need to use session_write_close() on the cron.php file. session_write_close
Are you using sessions? Every time you run session_start for given session, it is being locked, until the script finishes, or the session is being 'detached'.

AJAX call to check status of running process in the background, timing out

I've looked all over for any answer on this and haven't been able to find one, hopefully someone can point me in the right direction, I think I'm close
I have two hosts let's call them host1.mydomain.com and host2.mydomain.com (to get around the 2 concurrent connections per host/per browser issue), so they both point to the same content one is just an alias of the other
User goes to host1.mydomain.com, enters some information to register, clicks Go, which loads an iframe on the same page pointing to a page on host2.mydomain.com which calls a php script via exec("curl") sending the request to the background to start a website scraper, the process ID is then stored in the database for the user. After the iframe has successfully loaded (only takes 1 second since it's creating a background process) I have an AJAX request set on an interval to check the status periodically of the cURL process (by it's process ID in the database) so that I can display the current step of the scraper (there are 6 steps in total). All good so far.
The problem is that the AJAX requests are timing out after step 4 of the scraper (browser default timeout is 115/120 seconds) even though it shouldn't be because I'm working with two different hosts...meaning to say that it's almost as if I'm clogging both connections on host1.mydomain.com when I'm not because I initiated the scraper from host2
The iframe loads this URL: http://host2.mydomain.com/page.php
The contents of the PHP script calls:
exec("curl -o /dev/null 'http://host2.mydomain.com/page.php?method=process' > /dev/null & echo $!", $op);
Then my ajax request is polling http://host1.mydomain.com/status.php?pid=x which looks up in the database to check the status by the process ID
and once the scraper gets to step 4, my ajax requests are timing out
I think I confused myself explaining this, but hopefully someone can help me
Turns out I was successfully getting around the 2 connections per server/browser limitation...however in doing some research I found the reason why my ajax request was hanging is because I was trying to access and write to the session data from both of the requests. Digging a little deeper I found a session_write_close() which closes the session for reading/writing, I basically have to call this after each page request of the scraper and then reinitialize the session, this allows my ajax requests to go through and stops the blocking of the request.
Hopefully someone else finds this useful if you stumble across the same issue
Cheers!
Jeff
Instead of waiting for the request to finish, you should spawn new process which runs in the background on server. And use javascript to "check back" each few seconds to see when the execution has finished. Then all you have to do is pick up the result and display it.
Additionally you might want to make sure that only one php process is spawned.

How does facebook push data to news feed?

I am curious as to how Facebook pushes data to the browser as in the news feed. New data shows up at the top of the feed without reloading the page or clicking a button
Does Facebook achieve this by polling their server through AJAX at a set interval or do they somehow push new data from the server to the client unprovoked?
If so what language or API do they use to do this?
It's actually called 'long polling', or 'comet'. There are different ways to perform server push but the most common way is to keep connection open while it receives data (it has drawbacks as browser have limit of number of open connections to a host). Facebook had open-sourced the Tornado web servers, which can handle a lot of open connections (which can be a problem is you have a lot of users, but you are using apache for example). In the moment you receive AJAX response, you simply perform a new request, waiting for next response.
Essentially the code does an AJAX call to their servers, and either waits for a response which triggers another request, polls on a timer, or they open a websocket to receive data as soon as it's pushed. This of course is for "new" data showing up at the top of the feed. When the page is reached at the bottom, they just do another AJAX call to get the next n items.
They push it with AJAX, and they use (at least they USED to use), infinite scrolling.
So you'd load your page, and they'd make an initial call to the server to load some messages based on who is logged in, say with a framework like JQuery:
http://api.jquery.com/jQuery.ajax/
And then as you scroll down, they make note of when you're close to the bottom of the page and they need to load more so you're not left without data, and then they make another call automatically. This is called infinite scrolling, and keeps track of where you are in the DOM:
Just one example: http://ajaxian.com/archives/implementing-infinite-scrolling-with-jquery

Are PHP processes called via AJAX cancelled on "ESC"?

If I do an AJAX call, would the AJAX call be canceled if the user pressed "ESC" or leaved that page?
If the AJAX call gets canceled, would my PHP script continue to run until it finished what it was doing (provided time limit doesn't get reached or any other server configuration stops.), or would that process get killed at the same time as the Apache child it belongs to?
If the process does get killed with the Apache child even if it didn't finish, what would be the best way to keep that alive be or what other options should I consider? (ZendX_Console_Process_Unix not an option).
Thanks!
Later discoveries:
The AJAX call actually gets canceled if I hit "ESC" in Firefox (checked in firebug). The PHP process continues and is not affected by hitting ESC or closing the tab.
If the server process starts before the user ends the ajax call (closing the window, or moving to another website) then it will be carried out until its final result.
But an interrupted ajax call (meaning the transmission of data was not completed) the server will not process the call.
Note: hitting ESC will not end an ajax call per se, unless you javascripted that keypush behaviour.
One note... if you invoked your AJAX call from a hyperlink, using the href attribute, then YES, the user can press ESC and stop the transmission.
This can be canceled by pressing ESC
This can NOT be canceled by pressing ESC

Categories