0

I have a php based web application that captures certain events in a database table. It also features a visualization of those captured events: a html table listing the events which is controlled by ajax.

I would like to add an optional 'live' feature: after pressing a button ('switch on') all events captured from that moment on will be inserted into the already visible table. Three things have to happen: noticing the event, fetching the events data and inserting it into the table. To keep the server load inside sane limits I do not want to poll for new events with ajax request, instead I would prefer the long polling strategy.

The problem with this is obviously that when doing a long polling ajax call the servers counterpart has to monitor for an event. Since the events are registered by php scripts there is no easy way to notice that event without polling the database for changes again. This is because the capturing action runs in another process than the observing long polling request. I looked around to find a usable mechanism for such inter process communication as I know it from rich clients under linux. Indeed there are php extensions for semaphores, shared memory or even posix. However they all only exist under linux (or unix like) systems. Though not typically the application might be used under MS-Windows systems in rare cases.

So my simple question is: is there any means that is typically available on all (most) systems that can push such events to a php script servicing the long polling ajax request ? Something without polling a file or a database constantly, since I already have an event elsewhere ?

arkascha
  • 41,620
  • 7
  • 58
  • 90

1 Answers1

0

So, the initial caveats: without doing something "special", trying to do long polling with vanilla PHP will eat up resources until you kill your server.

Here is a good basic guide to basic PHP based long polling and some of the challenges associated with going the "simple" road: How do I implement basic "Long Polling"?

As far as doing this really cross-platform (and simple enough to start), you may need to fall back to some sort of simple internal polling - but the goal should be to ensure that this action is much lower-cost than having the client poll.

One route would be to essentially treat it like you're caching database calls (which you are at this point), and go with some standard caching approaches. Everything from APC, to memcached, to polling a file, will all likely put less load on the server than having the client set up and tear down a connection every second. Have one process place data in the correct keys, and then poll them in your script on a regular basis.

Here is a pretty good overview of a variety of caching options that might be crossplatform enough for you: http://simas.posterous.com/php-data-caching-techniques

Once you reach the limits of this approach, you'll probably have to move onto a different server architecture anyhow.

Community
  • 1
  • 1
Adam B
  • 3,775
  • 3
  • 32
  • 42