Reputation:
I currently have a piece of jquery/js code that runs every few seconds (5) a GET request, looking for new data that might of come in.
Is there some way I could get PHP to "push" or signal to the javascript code when new posts are available, rather then checking every few seconds if anything new came in?
Another example: I'm resizing an image for a user. I'd like to display real-time data to the user about the process going on - to display messages like "Uploading to the server", "Resizing your image", "Storing image".
Any help on something like this?
Upvotes: 1
Views: 489
Reputation: 44632
I'd recommend a SaaS solution, such as WebSync On-Demand; free for limited users, works with any server language, no hassles with setting up your own server, etc.
Upvotes: 1
Reputation: 1409
The simplest solution is to live with your currently implemented 5sec poll. This is the easiest implementation and works generally well.
Another option, is to implement a version of "long-polling"... where the javascript code opens the connection to the server and leaves it open (allowing the server to use that connection to send data to the client in a more immediate sense). When the client javascript detects the connection is closed (due to timeout), then it would just start another connection back to the server. The server code would need to be able to handle the quantity of long-polling clients, and handle the occasional disconnection of the clients (queuing messages for them when they re-connect).
Finally, there are the "comet" like solutions that would allow you to do server-side push to the client. I am not aware of a php-based Comet implementation...
Upvotes: 0
Reputation: 6147
you can use some sort of Comet technique, but that may require special considerations depending on your load. For example, if you are expecting a heavy load, you may need to configure your web server so that it can handle all of the concurrent connections. If you don't have that kind of control over your web server but expect alot of traffic, then it's best to stick with the polling technique.
but if you're just going to serve up a page to a small group... then try out one of those Comet techniques. there are jQuery plug-ins that can help:
Upvotes: 0
Reputation: 10290
Comet is can be exactly what you need.
It basically works by not letting the server respond immediately, keeping the possibility open to send data at the moment it gets in.
The problem is that apache and IIS currently aren't quite well in handling that much opened connections. Look at usobans answer.
Upvotes: 6
Reputation: 56391
This is something you do not wish to do. Be happy with the 5 second javascript poll. It really is the best way to do it.
Upvotes: 4