Skip to content

Instantly share code, notes, and snippets.

@aurelijusb
Last active October 1, 2016 12:07
Show Gist options
  • Save aurelijusb/dc2222dddf7d51ad78e6e0c2f0b7655b to your computer and use it in GitHub Desktop.
Save aurelijusb/dc2222dddf7d51ad78e6e0c2f0b7655b to your computer and use it in GitHub Desktop.
Servent-Sent events (1/2)

Code examples for presentation server-sent events

Run with

php -S 127.0.0.1:9001

Then open with browser: http://127.0.0.1:9001

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Server-Sent events</title>
</head>
<body>
<script type="text/javascript">
var source = new EventSource("/stream.php");
source.addEventListener('message', function(e) {
document.body.innerHTML += e.data + '<br/>';
}, false);
</script>
Welcome to<br/>
</body>
</html>
<?php
header('Content-type: text/event-stream');
for ($i = 0; $i < 10; $i++) {
foreach (['VilniusPHP', 'VilniusJs'] as $event) {
print 'data: ' . $event."\n\n";
ob_flush();
flush();
sleep(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment