title: Websocket onmessage event fired only once
body: <p>I'm using a basic WebSocket client within Angular 6 application.</p>
  
  <p>Everything works except for some reason both <code>socket.onmessage</code> or <code>socket.addEventListener('message'</code> are fired only once. No errors on console. and the message is defintiely sent to client (correct message is shown in websocket connection frames in browser console). All other events work correctly.</p>
  
  <p>Here is my class to work with websockets:</p>
  
  <pre><code>export class WebsocketService  implements OnDestroy {
    socket: WebSocket;
  
    constructor(@Inject(APP_CONFIG) private config: IAppConfig, private authService: AuthService) { }
  
    initConnection(): void {
      this.socket = new WebSocket(
        this.config.apiEndpoint.replace(/^https?:\/\//i, 'ws://') + 'ws/');
      this.watchEvent(WSEvent.ERROR).subscribe(data =&gt; {
        console.log(data);
      });
    }
  
    watchEvent(wsevent: WSEvent): Observable&lt;any&gt; {
      return new Observable(observer =&gt; {
        this.socket.addEventListener(wsevent, function (event) {
          observer.next(event);
        });
      });
    }
  
    onMessage(): Observable&lt;any&gt; {
      return new Observable(observer =&gt; {
        this.socket.onmessage = function (event) {
          observer.next(event);
        };
      });
    }
  
    ngOnDestroy() {
      // this.socket.close();
    }
  }
  </code></pre>
  
  <p><strong>wsevent:</strong> </p>
  
  <pre><code>export enum WSEvent {
    CLOSE = 'close',
    OPEN = 'open',
    MESSAGE = 'message',
    ERROR = 'error'
  }
  </code></pre>
  
  <p><strong>usage:</strong></p>
  
  <pre><code>this.wsService.watchEvent('message').subscribe((data) =&gt; {
     console.log(data);
  });
  
  
  this.wsService.onMessage().subscribe((data) =&gt; {
    console.log(data);
  });
  </code></pre>
  
  <p>In both cases <code>console.log(data);</code> shows me only the first message sent to the client. It doesn't matter how many times the message is sent and the content of the message - <code>onmessage</code> (refering to both options) is fired only once.</p>
  
  <p><strong>Update:</strong> 
  updated this post to contain actually working solution (the issue was that i called <code>observer.complete();</code> right after <code>observer.next(event);</code> which did what it is supposed to do)</p>