- flush: documentation ( source)
- time: documentation ( source)
- date: documentation ( source)
- sleep: documentation ( source)
- json_encode: documentation ( source)
- ob_flush: documentation ( source)
- header: documentation ( source)
<?php
/**
Ajax Streaming without polling
*/
//type octet-stream. make sure apache does not gzip this type, else it would get buffered
header('Content-Type: text/octet-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
/**
Send a partial message
*/
function send_message($id, $message, $progress)
{
$d = array('message' => $message , 'progress' => $progress);
echo json_encode($d) . PHP_EOL;
//PUSH THE data out by all FORCE POSSIBLE
ob_flush();
flush();
}
$serverTime = time();
//LONG RUNNING TASK
for($i = 0; $i < 10; $i++)
{
//Hard work!!
sleep(1);
//send status message
$p = ($i+1)*10; //Progress
send_message($serverTime, $p . '% complete. server time: ' . date("h:i:s", time()) , $p);
}
sleep(1);
send_message($serverTime, 'COMPLETE', '100');