Ajax push system
In my own system, I have a PHP
page which displays all the goods I have sold through my online webshops, from the latest transaction to the first.
I would like this page to automatically update, whenever people buy something from me.
I could make AJAX
call every 5 seconds to check the orders in my databases, but that seems 1980?
or is it that way people do it?
How can I go about pushing a notification to my php
page whenever my php newOrder() function (lets call it that) is called?
Solution 1:
You can achieve push within PHP but it won't be the most efficient solution because to achieve push you need to maintain long running connections between your client and your server (HTTP or WebSocket connections).
See:
- Long Polling/HTTP Streaming General Questions
- phpwebsocket
- php-websocket on github
- Ratchet
- how to implement comet in PHP - frequently linked to guide
General best practice when building a realtime infrastructure has been to decouple the push solution from your web application (Note: node.js and socket.io has changed this a bit, but personally I still think it should be decoupled). But, assuming that the latter is still the best solution you would need to write/host/install this push solution. Decoupling also means that the technology doesn't have to be PHP but you can access/use it from PHP. Maybe a bit of overkill? And especially if you don't have too many users on your site?
For simplicity I would recommend looking at using a 3rd party hosted service. I work for one such company called Pusher. Using a service such as ours lets you remove the need to install and maintain the realtime part of your application. It also makes it really easy to add the push functionality you are looking for. All you need to do is add a few lines of PHP code to your existing app to trigger the push notifications and add a few lines of JavaScript to your front-end.
Resources:
- Most commonly use PHP library for this: https://github.com/pusher/pusher-php-server
- Quickstart guide
If you'd like to investigate the alternatives or some of the technologies I've mentioned above I'm maintaining a list of realtime technologies which you might also be interested in.
Solution 2:
You could simulate a push effect by doing the following (pseudo code!)
// This would go on for 60-90 seconds, unless there is new data
for($i = 1; $i <= 60; $i++) {
// Check in the database what the last timestamp is
// Compare this to the timestamp that has been posted by the AJAX-call
// If there is something new, show the data and then exit
// If not, sleep for 1-3 seconds
}
Your javascript:
function pollForNewProducts() {
// Check if there is a timestamp
// Make an AJAX-request to the script that has the first code in it
// If there is a response (JSON?) than evaluate that
// If not, then run the script again
pollForNewProducts();
}
It is a simple yet effective way to let the server do all the hard work, instead of timeouts on the client side which will cause the browser to eat memory.
More about this on:
Simple “Long Polling” example code?
Client notification, should I use an AJAX Push or Poll?
PHP or Javascript poll code
ExtJS 3.0: Ext.Direct with PHP: Polling:ok and Pushing ?
Comet Programming: Using Ajax to Simulate Server Push
Solution 3:
Apart from the excellent suggestion about nodejs ..if you want to still use php to achieve this, you want to look for the COMET method, not ajax.
Howto with php here : http://www.zeitoun.net/articles/comet%5Fand%5Fphp/start