public
Created

  • Download Gist
event-source.php
PHP
1 2 3 4 5 6 7 8 9
<?php
header("Content-Type: text/event-stream\n\n");
 
echo 'data: ' . json_encode(
array(
'from' => 'event-source'
)
) . "\n";
?>
renderer.html
HTML
1
<script src="renderer.js"></script>
renderer.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13
var worker = new Worker('worker.js');
 
worker.addEventListener('message', function (event) {
var message = event.data;
 
console.group('Message Received');
console.log( message );
console.groupEnd();
 
}, false);
 
// This will trigger a message event in the worker
worker.postMessage(true);
worker.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12
self.addEventListener('message', function (event) {
var thisp = this,
eventSrc = new EventSource('event-source.php');
 
eventSrc.addEventListener('message', function (event) {
var message = JSON.parse(event.data);
thisp.postMessage(message);
 
}, false);
}, false);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.