1

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 Answers5

6

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.

Community
  • 1
  • 1
Dykam
  • 9,923
  • 4
  • 25
  • 32
  • 1
    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 Jul 14 '09 at 16:26
  • 1
    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 Jul 14 '09 at 16:26
  • 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 Jul 14 '09 at 16:27
  • 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 Jul 14 '09 at 16:28
  • @Dykam: excellent point. It's a trade-off. IMO polling wins that one. – Randolpho Jul 14 '09 at 16:30
  • Sure, you can use existing server software, but performace will be quite poor. – usoban Jul 14 '09 at 16:31
  • Yeah. I'll edit the post, as it is a little too optimistic :P. – Dykam Jul 14 '09 at 16:33
4

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
  • 52,575
  • 15
  • 139
  • 173
  • 3
    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 Jul 14 '09 at 16:56
  • any reasons? at least briefly? –  Jul 14 '09 at 16:56
1

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
  • 42,243
  • 5
  • 64
  • 107
0

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:

Comet and jQuery

Community
  • 1
  • 1
Nick Franceschina
  • 5,469
  • 6
  • 32
  • 50
0

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
  • 1,339
  • 1
  • 8
  • 8