title: How to run EventSource inside Shared Worker?
body: <p>I implemented <a href="https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events" rel="nofollow noreferrer">server-sent events</a> to receive messages from the server. I describe how I implemented it on <a href="https://stackoverflow.com/questions/31143150/is-there-a-way-to-make-using-server-sent-events-persistent">another question</a></p>
  
  <p>Now I am trying to run it inside a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Shared_workers" rel="nofollow noreferrer">shared worker</a> to prevent the user from opening multiple connections to the servers via multiple browser tabs.</p>
  
  <p>Here is what I have done to run the code inside a Shared Worker</p>
  
  <p>created a file calles <code>worker.js</code> and I put this code inside of it</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Then when the HTML page loads I call this code </p>
  
  <pre><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);
  
      });
  </code></pre>
  
  <p>But the code is not returning messages from the server.</p>
  
  <p>How can I correctly run <code>EventSource</code> event inside the shared worker?</p>