Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<script src="event-source-2.js"></script>
document.addEventListener('DOMContentLoaded', function () {
var eventSrc = new EventSource('event-source-2.php');
console.group('EventSource Created');
console.dir( eventSrc );
console.groupEnd();
eventSrc.addEventListener('open', function (event) {
console.time('Time Elapsed between open and message');
console.log(event.type);
});
eventSrc.addEventListener('message', function (event) {
console.timeEnd('Time Elapsed between open and message');
console.log(event);
console.dir( JSON.parse(event.data) );
});
}, false);
<?php
header("Content-Type: text/event-stream\n\n");
// despite not having the while(true){}
// this seems to repeat pushing messages to the client
echo 'data: ' . json_encode(
array(
0 => array(
'time' => time(),
'message' => 'Some kind of foo'
),
1 => array(
'time' => time(),
'message' => 'Some kind of quux'
)
)
) . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment