How to use RxJS websocket in web worker in angular

I have already implemented websocket in my project but browser performance is very slow and sometimes it freezes browser/browser tab as well. So thinking of using websocket in web worker. How can I use the below code with web worker in my project.

**.service.ts**

 connect(): Observable<any> {
  return of(wsURL).pipe(
    map(apiUrl => apiUrl.replace(/^https/, 'wss') + '/stream'),
    switchMap(wsUrl => {
      if (this.connection$) {
        return this.connection$;
      } else {
        this.connection$ = webSocket(wsUrl);
        return this.connection$;
      }
    })
  );

  closeConnection() {
    if (this.connection$) {
      this.connection$.complete();
      this.connection$ = null;
    }
  }

**component.ts**

ngOnInit() {
  this.webSocket.connect().pipe(
    takeUntil(this.destroyed$)
  ).subscribe(messages => this.messages.push(messages));
}

ngOnDestroy() {
  this.destroyed$.next();
}


from Recent Questions - Stack Overflow https://ift.tt/3cocI7q
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations