Dr.Kameleon
Dr.Kameleon

Reputation: 22810

Notify Ajax/Javascript that background PHP has completed

OK, here's the situation - simple and clear :

So, how could Page A get notified that back.php has finished?

(I've thought e.g. of firing an ajax request every 1-2 secs to a script checking the db for back.php's progress, but that seems pretty weird and definitely not efficient...)

Any ideas?

Upvotes: 2

Views: 645

Answers (2)

Daniel
Daniel

Reputation: 1692

You could do a long-polling or short-polling technique which is quite common and not so weird. It does make a lot of unnecessary requests, but it's the simplest and most supported method out there.

The second method would be using web sockets. Now web sockets through html5 are not generally supported as of now, but an alternative would be to use a library that takes care of fallbacks to other methods such as flash, or even long-polling if your browser / device doesn't support a previous method. A really good library for real-time communication would be Socket.IO. It allows many different implementations of real-time or close to real-time, it also allows you to choose which method of communication you want to use. Some cloud based web services such as Paas don't widely support web sockets. Libraries will go around this by letting you choose or force a specific method of communication: Web Sockets, Flash Sockets, Long-Polling, etc..

But since your using php, you could try this: https://github.com/lemmingzshadow/php-websocket

Though as mentioned on some of these sites, using websockets with php is not as straight forward as node.js or twisted python. But nonetheless it's achievable and does work at some degree. I found that having a node.js backend to do the real-time stuff is a lot easier then trying to force a technology that is not meant to be worked in that way.

Upvotes: 6

Alex Weinstein
Alex Weinstein

Reputation: 9891

What you're basically looking for is Comet. Here's a good thread on it:

Using comet with PHP?

Upvotes: 1

Related Questions