title: re-subscribing a prevously unsubsribed Rx.Subject in Angular 2
body: <p>I am not sure, if i am using a wrong approach or i just coding badly but to explanation of my problem.</p>
  
  <p>I have a backend component, where i manage array of websockets ( because every websocket can have a different WS url, or they need to be turned off/on separately) and for every websocket, I want to know if its state and send it to a view trough Rx.Subject (if its opened, closed or if it dont have required data for making a connection)</p>
  
  <p>which looks </p>
  
  <pre><code>public connectDeviceTerminalWebSocket(server: string, port: string): void {
      if (!(server !== null) &amp;&amp; !(port !== null)) {
  
          let websocket: WebSocket = null;
  
          let wsPosition: number = this.hardwareTerminalwebSockets.findIndex(ws =&gt; {
              if (ws.url.includes(server + ':' + port)) {
                  websocket = ws;
                  return true;
              }
          });
  
          if (websocket) {
              this.closeHardwareTerminalWebsocket(websocket.url);
          }
  
          websocket = new WebSocket(`${this.wsProtocol}://${server}:${port}/${this.getToken()}`);
  
          websocket.addEventListener('close', ws =&gt; {
              this.reconnectTerminalWebSocketAfterTimeout();
              this.hardwareTerminalState.next({ 'websocketUrl': websocket.url, 'isConnected': false, 'reason': 'conectionFailed' });
          });
  
          websocket.addEventListener('open', ws =&gt; {
              this.reconnectTerminalWebSocketAfterTimeout();
              this.hardwareTerminalState.next({ 'websocketUrl': websocket.url, 'isConnected': true, 'reason': 'connected' });
          });
  
  
          let opened = Rx.Observable
              .fromEvent&lt;void&gt;(websocket, 'open');
          let channelReceived = Rx.Observable
              .fromEvent&lt;MessageEvent&gt;(websocket, 'message')
              .map(event =&gt; {
                  try {
                      return JSON.parse(event.data);
                  } catch (e) {
                      console.error('Parse error: ', e);
                  }
                  return null;
              });
          channelReceived
              .filter(message =&gt; message.message_channel === 'hardware-logger')
              .subscribe(this.hardwareTerminal);
  
  
          opened.subscribe(open =&gt; this.sendWebSocketTerminalMessageQueue());
  
          if (wsPosition &gt; -1) {
  
              this.hardwareTerminalwebSockets[wsPosition] = websocket;
          } else {
  
              this.hardwareTerminalwebSockets.push(websocket);
          }
      } else {
          this.hardwareTerminalState.next({ websocketUrl: null, isConnected: null, 'reason': 'cantConnect' });
          return;
      }
  }
  </code></pre>
  
  <p>and this works.</p>
  
  <p>In my view, I have defined</p>
  
  <pre><code>hardwareTerminalStateWS: Rx.Subject&lt;ITerminalWebsocketMessage&gt;;
  </code></pre>
  
  <p>which i only subscribe once this way </p>
  
  <pre><code>this.hardwareTerminalStateWS.subscribe(msg =&gt; this.onStateMessage(msg));
  </code></pre>
  
  <p>and in ngOnDestroy in my view</p>
  
  <pre><code>this.hardwareTerminalWS.unsubscribe();
  </code></pre>
  
  <p>to unsubsribe it.</p>
  
  <p><strong>Now the main problem</strong> if i close/change to a different view, and go back (eg. re-opening and subscribing that again) it throws error </p>
  
  <p><strong>"Error in ./hwcomponent class hwComponent - inline template:47:28 caused by: object unsubscribed"</strong></p>
  
  <p>Should i change the Rx.Subject to a different class(Observer?)? Or use a different method to close hardwareTerminalStateWS?
  Because if i dont close that, the next time i open view again, its open a second hardwareTerminalStateWS and read every messeage twice.</p>