3

I implemented server-sent events to receive messages from the server. I describe how I implemented it on another question

Now I am trying to run it inside a shared worker to prevent the user from opening multiple connections to the servers via multiple browser tabs.

Here is what I have done to run the code inside a Shared Worker

created a file calles worker.js and I put this code inside of it

self.addEventListener('getMessagingQueue', function (event) {
    console.log('Listener is Available');
    var thisp = this;
    var eventSrc = new EventSource('poll.php');

    eventSrc.addEventListener('getMessagingQueue', function (event) {

    var message = JSON.parse(event.data);

    thisp.postMessage(message);

    }, false);

}, false);

Then when the HTML page loads I call this code

$(function(){       

        var worker  = new SharedWorker('worker.js');

        worker.addEventListener('getMessagingQueue', function (event) {
          var message = event.data;

          console.group('Message Received');
          console.log( message );
          console.groupEnd();

        }, false);

    });

But the code is not returning messages from the server.

How can I correctly run EventSource event inside the shared worker?

Community
  • 1
  • 1
Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • What is `getMessagingQueue`? Afaik, workers only emit `message` and `error` events. – Bergi Jun 30 '15 at 18:24
  • @Bergi thank you for your comment. the `getMessagingQueue` is the name of the event returned by poll.php – Jaylen Jun 30 '15 at 18:25
  • Yeah, on the `EventSource`, but you cannot use the same event name on the worker - there's only `onmessage`. – Bergi Jun 30 '15 at 18:29
  • Got you, okay better, but I get this error in the console `ReferenceError: EventSource is not defined` which is referencing line 5 of the `worker.js` – Jaylen Jun 30 '15 at 18:32
  • Hm, [looks like](https://developer.mozilla.org/en-US/docs/Web/API/Worker/Functions_and_classes_available_to_workers) `EventSource` is not supported in workers :-/ – Bergi Jun 30 '15 at 18:37
  • 1
    This post show how they implemented it but I am unable to replicate his code https://gist.github.com/rwaldron/552549 – Jaylen Jun 30 '15 at 18:39
  • Maybe the support is browser-specific. You might want to contact Rick on how he made that gist work. – Bergi Jun 30 '15 at 18:41
  • does it make more since to use WebSocket? – Jaylen Jun 30 '15 at 18:43
  • `WebSockets` seem to be supported in Workers at least, yes. – Bergi Jun 30 '15 at 18:44
  • …though actually SSE [should work](https://lists.w3.org/Archives/Public/public-webapps/2012AprJun/0263.html) as well – Bergi Jun 30 '15 at 18:48
  • Hum, so why is it not working for me? – Jaylen Jun 30 '15 at 18:52
  • 1
    Firefox currently does not support `EventSource` in web/shared workers [Bug 876498](https://bugzilla.mozilla.org/show_bug.cgi?id=876498) – Ricardo Jul 02 '15 at 00:33
  • [Browser support](http://caniuse.com/#feat=eventsource) – Ricardo Jul 02 '15 at 00:35
  • Possible duplicate of [How to make EventSource available inside SharedWorker in FireFox?](http://stackoverflow.com/questions/32321295/how-to-make-eventsource-available-inside-sharedworker-in-firefox) – user Mar 25 '16 at 00:26

0 Answers0