6

For a while I've been playing with the idea of long polling for my notification system, but i've never been able to think of a way to make it more efficient for my backend.

Most implementations I have seen hold the connection open, and the php queries the database server every few seconds to see if new data has been aded. This strikes me as no better than having the javascript repetitively poll the server.

In either case I have my database servers being hit tens thousands of times, which is understandably not particularly desirable.

Are there any systems in place that could 'alert' the executing/sleeping long polling script to the new data?

1 Answers1

2

Single system

If your application is the only system that changes the database, then you can trigger your listeners only when your application performs changes (ideally only for changes to the entities which are interesting for each listener).

Multiple systems

In the case of multiple systems accessing/changing the database, you can either

  • work with database triggers

or if you don't want to do that (I usually avoid it), you can either

  • make sure that all other systems accessing the database will always notify your application (via some messaging mechanism)

or if that's not possible

  • you can at least optimize by having only one loop that queries the database frequently, and informs all listeners at once (so you don't have a loop for each listener).
Chris Lercher
  • 36,020
  • 19
  • 96
  • 128
  • I'd love for a system such as database notification to work (ajax long polling in a web application). I'm still a little hazy on how this could operate. The current loop, server currently checks for new entries in the database, then checks and flushes if new data is present. I'm not sure how the a database event would reach the sleeping process, unless it was to again poll the backend. Maybe the way I am doing it **is** the most efficient? – Charles Harris Aug 03 '10 at 08:39
  • @Charles: Yes, this could be a little problem in PHP! I think, this thread contains some answers: http://stackoverflow.com/questions/603201/using-comet-with-php – Chris Lercher Aug 03 '10 at 10:33