title: Reconnecting a WebSocket without creating a new instance
body: <p>So I have created a new web application that uses a WebSocket to Node.JS WebSocket server. Now the Node server does everything it needs to and works perfectly. My problem is with the browser implementation.</p>
  
  <p>I have looked at a lot of the libraries other people have been given in the answers, but I want to see if their is a better or cleaner way to do this.</p>
  
  <p>So in my instance, I essentially create an object, which wraps the WebSocket and then uses a setInterval to reconnect, by calling connect(<strong>url</strong>), which will create another WebSocket instance.</p>
  
  <p>I have been looking at connections and clients, and it seems that if a connection closes, say the server goes down, or something happens, it looks as though sometimes, in a longer period of time, the WebSocket connections are doubling up, so instead of 1 connection per client, it is 2, 3 or 4...?</p>
  
  <p>I have a feeling this is because I am making a new instance of the WebSocket each time?</p>
  
  <p>Code below:</p>
  
  <pre><code>// Main Function
  function WSWrapper() {
  
      // Variables
      this.socket = null;
      this.enabled = false;
      this.retry = false;
  
      // Connect
      this.connect = function(address) {
  
          // Sets the address
          this.address = address;
  
          // Creates the websocket connection
          this.socket = new WebSocket(address);
  
          // On message event handler
          this.socket.onmessage = function(event) {
  
              // Do stuff here
  
          }
  
          this.socket.onopen = function(event) {
  
              // On connect, disable retry system
              window.ta.enabled = true;
              window.ta.retry = false;
  
          }
  
          this.socket.onclose = function(event) {
  
              // On close, enable retry system, disable bidding
              window.ta.enabled = false;
              window.ta.retry = true;
              window.ta.bidEnabled = false;
  
          }
  
          this.socket.onerror = function(event) {
  
              // Set variables off
              window.ta.enabled = false;
              window.ta.bidEnabled = false;
              window.ta.retry = true;
  
          }
  
          return true;
      }
  
  
      // Close Socket
      this.closeSocket = function() {
  
          // Shutdown websocket
          this.socket.close();
          return true;
  
      }
  
      // Send Message
      this.socketSend = function(content) {
          this.socket.send(content);
          return true;
      }
  
      // Retry System: Attempts to reconnect when a connection is dropped
      this.repeat = setInterval(function() {
          if (window.ta.enabled == false &amp;&amp; window.ta.retry == true) {
              window.ta.connect(window.ta.address);
          }
  
      }, 2000);
  
  }
  
  window.ta = new WSWrapper();
  window.ta.connect('wss://example.com');
  </code></pre>
  
  <p>I have come up with some thoughts and questions, any answers would be good.</p>
  
  <ol>
  <li><p>Is there a way to reconnect the same socket? like an .open(<strong>url</strong>) function that will re-open the connection? I looked in the chrome console, and went through the prototype for the WebSocket, but I see nothing there, so I don't think so but would love to be told otherwise.</p></li>
  <li><p>Could I fix this by using some functions that take the information, so for example, I create the WebSocket instance and then pass all requests to another function to manage the message information, and then when a connection disconnects, I can <em>somehow</em> delete the old instance and re-create a new one?</p></li>
  </ol>
  
  <p>Anything would be good, as I am really not sure, it seems that everyone makes a wrapper (like I am doing), but does things differently, so what is the best way or the preferred way, that won't cause multiple instances of the socket to keep running? If there is a problem with my code then please explain!</p>
  
  <p>Thanks</p>