Stopping AJAX request on server side

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
moliate
Forum Newbie
Posts: 2
Joined: Sat Jun 27, 2009 3:29 pm

Stopping AJAX request on server side

Post by moliate »

Hi everyone,

I have an application that relies on PHP for processing some AJAX requests. In some cases the processing time might be quite long and the user could initiate a new request before the server has a chance to respond. Is there a way to interrupt the previous call?

Since apache handles the requests it does not seem to be a trivial task. What I've tried so far:
  • Calling 'XMLHttpRequest.abort()' - does not seem to send anything to the server. Furthermore the client gladly accepts "aborted" responses.
    Spawning my own process using stuff like system() and the process library (a bad idea in a webserver environment...)
    Trying to track the PID of the apache spawned process and kill it when a new request is made (no - even worse!!)
    Trying to create a "session singleton" that ensures that only one request per session can be made (working on it, but it seems hackish at best)
Any ideas would be appreciated!

Thanks/
moliate
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Stopping AJAX request on server side

Post by requinix »

It'd be a lot nicer if you could abort the new call.

Typical Unix way of tracking instances of things is to use a PID file. When the process starts up it puts its process ID into a certain file; every time it starts the program first looks for the file and, if there is one, gets the PID and does something (display a message and quit, send a message to the previous instance, etc).

In your case you don't really need the PID (killing off the older process isn't a smart way to do this) so a simple flag will do.
Stuff that flag somewhere, like a database. Every time the request goes through the script looks for the flag in the database and does something if it's present. Otherwise it continues: sets the flag, does its work, and removes the flag. It's not a perfect scheme but it would take two near-simultaneous requests to get around it.

For what you originally wanted you'd need a counter instead of a flag. When the process starts it increments the counter and remembers the new value. Occasionally it looks at the counter and compares it to the stored value: if it changed (went up) then another process began and the current one should abort.
That means a lot of counter-reads and can be a really bad idea in some circumstances.

The more advanced way would be to use a) shared memory, or b) messaging (aka IPC). If you're serious about this idea then those are the best solutions.
moliate
Forum Newbie
Posts: 2
Joined: Sat Jun 27, 2009 3:29 pm

Re: Stopping AJAX request on server side

Post by moliate »

Thanks tasairis,

Your reply indicates that I'm at least on the right path. I'm currently working on the following idea:

1) When a process starts it uses shm_attach() to connect to some shared memory. It uses a hash of the current session as an identifier.
2) It then writes a request-unique flag to the shared memory before starting processing.
3) During processing it polls the memory for its own flag - if it finds something else it detaches from the memory and quits.
4) When processing is finished it uses shm_remove() from the shared memory.

I think shared memory would be reasonably fast to allow polling.

If anyone has any comments to the solution above I'll be glad to hear about it. I'm not very used to PHP programming and I don't have much experience with a webserver environment.

/moliate
Post Reply