title: Javascript: Properly Globalizing a Websocket Send Function
body: <p>I am working with a WebSocket and trying to be able to send socket data at anytime from throughout my application. When I attempt to access the send command from within another function, I am receiving:</p>
  
  <pre><code>Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.
  </code></pre>
  
  <p>This only is occuring when I call a function, this is how I am setting up my websocket:</p>
  
  <pre><code>Main.socket = (function() {
  var socket = new WebSocket("ws://server:port");
  
  socket.onopen = function() {  
      console.log("Socket has been opened!");  
  }
  
  function send() {
      socket.send('test');
  }
  
  return {
      socket: socket,
      send: send
  }
  })();
  </code></pre>
  
  <p>I am able to call the function globally, and also when I console.log Main.socket from within a function it is able to see the socket. But when I call the send function I get that error.</p>