Create a gist now

Instantly share code, notes, and snippets.

header("Content-Type: text/event-stream\n\n");
echo 'data: ' . json_encode(
'from' => 'event-source'
) . "\n";
<script src="renderer.js"></script>
var worker = new Worker('worker.js');
worker.addEventListener('message', function (event) {
var message =;'Message Received');
console.log( message );
}, false);
// This will trigger a message event in the worker
self.addEventListener('message', function (event) {
var thisp = this,
eventSrc = new EventSource('event-source.php');
eventSrc.addEventListener('message', function (event) {
var message = JSON.parse(;
}, false);
}, false);

Hi Rick,

I took your code and did a copy and paste but I can't seem to get it to work. I keep getting this error in the console. ReferenceError: EventSource is not defined. This error reference line 5 in the worker.js file.

I appreciate your help and I am trying my best to implement EventSource into a worker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment