Skip to content

Instantly share code, notes, and snippets.

@marcmartino
Created July 2, 2012 21:38
Show Gist options
  • Save marcmartino/3035875 to your computer and use it in GitHub Desktop.
Save marcmartino/3035875 to your computer and use it in GitHub Desktop.
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
/**
* Constructs the SSE data format and flushes that data to the client.
*
* @param string $id Timestamp/id of this connection.
* @param string $msg Line of text that should be transmitted.
*/
function sendMsg($id, $msg) {
echo "id: $id" . PHP_EOL;
echo "data: $msg" . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
}
$serverTime = time();
sendMsg($serverTime, 'server time: ' . date("h:i:s", time()));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment