views:

219

answers:

5

Hi, 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?

+5  A: 

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.

Randolpho
+1 totally agree
Michal M
Not terribly informative though. The reason you dont want to do 'push' is that it is extremely resource intensive, it costs the server and it can cost the client connectivity resources. If the 5 second polling works then use it, only switch if you need things faster than that like a high value in-house stock broker intranet web app.
Karl
any reasons? at least briefly?
got it. thanks.
+6  A: 

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.

Dykam
There are a lot of cons to using Comet or Comet-like systems, IMO. Unless you have near-unlimited resources like Google, then keeping that connection open indefinitely kinda works. In most real-world situations, the server will be dragging. Still, +1
Randolpho
Be noticed that Comet is not that easy to implement, besides you can't use Apache or ISS, but specialized server software, like Meteor.
usoban
True, I know those drawbacks. But polling every five seconds can also be a hit for the performance, as a new connection has to be set up and stuff every time.
Dykam
usoban, it depends a little on the scale and type of the implementation. I can implement a basic, rough version on my server, but it won't scale well.
Dykam
@Dykam: excellent point. It's a trade-off. IMO polling wins that one.
Randolpho
Sure, you can use existing server software, but performace will be quite poor.
usoban
Yeah. I'll edit the post, as it is a little too optimistic :P.
Dykam
A: 

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:

http://stackoverflow.com/questions/136012/comet-and-jquery

Nick Franceschina
A: 

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...

jeremyasnyder
A: 

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.

jvenema