Skip to content

@rwaldron /event-source.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
<?php
header("Content-Type: text/event-stream\n\n");
echo 'data: ' . json_encode(
array(
'from' => 'event-source'
)
) . "\n";
?>
<script src="renderer.js"></script>
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);
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);
@malhayek2014

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
Something went wrong with that request. Please try again.