Skip to content

Instantly share code, notes, and snippets.

@baniol
Created December 9, 2013 15:34
Show Gist options
  • Save baniol/7874055 to your computer and use it in GitHub Desktop.
Save baniol/7874055 to your computer and use it in GitHub Desktop.
html5 server sent events, push
<!DOCTYPE html>
<html>
<head>
<title>Template html</title>
</head>
<body>
<!-- FROM: http://www.w3schools.com/html/html5_serversentevents.asp -->
<div id="result"></div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
if (typeof(EventSource) !== "undefined") {
var source = new EventSource("server.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment