title: Chrome extension long-lived message connection - how to use callback functions?
body: <p>For a while I've been using <code>chrome.runtime.sendMessage(message, callback)</code> for messages from my content script that need to run a callback function after receiving a response from the background.</p>
  
  <p>I've also got a long-lived connection that is used for sending messages from the background to the content script (without it being a response to a message initiated by the content script):</p>
  
  <pre><code>backgroundPort = chrome.runtime.connect({ name: "contentScript" });
  backgroundPort.onMessage.addListener(function(message){
    if (message["action"] == "something"){
      // Do stuff
    }
  });
  </code></pre>
  
  <p>As far as I can tell, <code>backgroundPort.postMessage()</code> does <strong>not</strong> support callback messages which means messages that have a callback need to use <code>chrome.runtime.sendMessage()</code>.</p>
  
  <p>The problem with this approach is that there is a lot of overhead in setting up a new connection between content script/background for each message, so I'm trying to get callback functionality into the existing long-lived connection through <code>backgroundPort</code>, but it gets kind of messy.</p>
  
  <p>Anyone who's been able to come up with an elegant solution?</p>