title: Chrome extension - Messaging with multiple ports
body: <p>In my extension, different content scripts connect to background.js to communicate with each other. The problem I'm having, is that when I receive a message on one port, I am not able to forward this information to the other port. The error I'm getting is:</p>
  
  <pre><code>Error: Attempting to use a disconnected port object
  </code></pre>
  
  <p>My code in background.js</p>
  
  <pre><code>chrome.runtime.onConnect.addListener(function(port) {
    if(port.name === "menuport") {
      port.onMessage.addListener(function(msg) {
        console.log('BGJS from menuport')
        console.log(msg)
        var page_port = chrome.runtime.connect({name: "pageport"});
        page_port.postMessage(msg);
      });
    } else if (port.name === "pageport") {
      port.onMessage.addListener(function(msg) {
        console.log('BGJS from pageport')
        console.log(msg)
      });
    }
  });
  </code></pre>
  
  <p>The pageport is activated in content.js:</p>
  
  <pre><code>var page_port = chrome.runtime.connect({name: "pageport"});
  page_port.postMessage({source: "page", status: "ready"});
  page_port.onMessage.addListener(function(msg) {
      console.log(msg)
  });
  </code></pre>
  
  <p>The menuport works with a similar script. The activation of both ports works, I can see in the console. However I can't post a message to a port from background.js.</p>