There is no provision in the http protocol for server initiated events. To collect real time data without resorting to socket communications there are essentially only two methods; server polling, and simulated server push.
With library support for JSON, Xanjax now provides a suitable base framework to enable simulated server push; which is far superior to polling techniques in minimising client resources, and in reducing both network traffic and network latency.
One characteristic of simulated server push (SSP) techniques is that they are achieved by the server holding open (keeping alive) and blocking an http request until server data becomes available. A potential downside is that each KeepAlive connection consumes a server thread; this should be carefully considered before using SSP on sites that may become heavily loaded.
SSP requires mutually co-operative client and server scripting. It prescribes three main conditions: 1. asynchronous client connection (AJAX and friends), 2. asynchronous server processes (a foreground process must maintain client http connection while an asynchronous background process manages and responds to server events), and 3. session tracking must exist across recurrent http connections and across both server processes.
Xanjax uses AJAX client side, employing JSON (JavaScript Object Notation) for compact rapid data transfer and simple data retrieval. A customised AJAX thread retrieves JSON data and maintains the session by returning a session signature. It also starts the process, writes data to a browser DOM object as it arrives, and responds to a termination event.
Server side, Xanjax presently offers two PHP process pairs giving the choice of either shared memory, or file based inter-process communications (IPC). The demo on this page uses shared memory IPC.
If you want to understand the gory technical details, please explore the commented code in xanjax sitekit. Xanjax client side javascipt uses function jsonPushlet which depends on xanXHR and xanjaxHttpRequest for async comms, with objfyJSON, filterList, and newElement for JSON handling, data filtering and appending. You can find these functions either in xanjax.js or xanjson.js
There's a couple of gotcha's when setting up PHP/Apache to use PHP Server Pushlets! Firstly, if you're setting this up for the first time, you'll need to make sure that PHP Safe Mode is disabled (Note: PHP Safe Mode is now deprecated anyway - it seems it wasn't actually very "safe"). Secondly, you must never put any code or text before PHP header statements; failure to observe this leads to code that works or not depending on PHP/Apache versions and configuration, and it's almost impossible to track down - be warned!
On the server I'm using mpushlet.php and bg_mpush.php, which are the foreground and background PHP processes for shared memory IPC respectively (fpushlet.php and bg_fpush.php can be used for file based IPC). Note that PHP itself is not multi-threaded and cannot concurrently run PHP processes - what is happening is that the second PHP process is running in a second (CLI) instance of PHP, launched via a Linux shell call. Linux is the vehicle for multi-threading here, not PHP.
Before experimenting with the code, I recommended installing xanjax sitekit, and using it to verify your PHP/Apache configuration. There is no particular reason why this would not work on other Web Server/OS combinations; please post a descriptive comment to the xanjax blog if you have success. This may assist others who either don't wish or aren't able to use Apache/Linux.