I have a kind of application that must query a db at periodic intervals..I want to use push technology such that when the database is updated, the server sends data to the client..How is it possible to implement that ?
4
-
3PHP is not appropriate for this, as you need to keep a request open for each client.Ikke– Ikke2012-01-24 14:15:10 +00:00Commented Jan 24, 2012 at 14:15
-
2Or, you fake it by polling the server with JavaScript every few seconds. It is not a true push, but depending on the timeliness of the data, that may be okay.Michael Berkowski– Michael Berkowski2012-01-24 14:21:56 +00:00Commented Jan 24, 2012 at 14:21
-
This needs way more context since it depends a lot on the rest of the architecture, the type of client and the event that can trigger an update.deceze– deceze ♦2012-01-24 14:22:13 +00:00Commented Jan 24, 2012 at 14:22
-
I peg to disagree with PHP not being suitable - there is nothing in PHP, that makes long poll AJAX and friends unfeasable, in fact I do it all the time (update the seating plan in a ticket-selling app, when a seat has been sold at another boxoffice)Eugen Rieck– Eugen Rieck2012-01-24 14:30:13 +00:00Commented Jan 24, 2012 at 14:30
Add a comment
|
2 Answers
We do this quite regularily - our way is "long polling":
- From the client, start an async poll request
- On the server, just keep this poll request waiting for e.g. 30secs (send a \0 or a blank every couple of seconds, if you need really long times)
- When the database is updated, create some sort of event on the server side
- When such an event is detected by a long-poll handler, write the resulting client notification and end the request
- On the client, when an asyn poll ends, check it for notifications, work on them, then start the next long poll
3 Comments
Eugen Rieck
Typically Apache, typically LAMP stack, typically Ubuntu Server LTS
thelastshadow
And you don't have trouble with long polling stealing your processes?
Eugen Rieck
No, no problems if you follow some simple rules: 1. Longpoll handlers should not use much memory - keep them as basic as possible until the really do some work. 2. have an eye on your connection count and tune your apache if necessary