Charles Harris
Charles Harris

Reputation: 71

AJAX long polling, increasing efficiency

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?

Upvotes: 6

Views: 1285

Answers (1)

Chris Lercher
Chris Lercher

Reputation: 37788

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

Upvotes: 2

Related Questions