I have created a angular project in which my web socket connection is getting open after reloading the page otherwise it does not open. I want to start a socket as soon as user logs in and close the socket as it logs out.
Below is the socket service I created using rxjs web sockets not socket.io :
SOCKET_URL: string = environment.webSocketEndpoint;
ws: WebSocketSubject<any>;
open() {
this.ws = webSocket(`${this.SOCKET_URL}/?access_token=${this.auth.user()?.['accessToken']}&platform=web`);
}
emit(event: Object): void {
this.ws.next(event);
}
events(): Observable<any> {
if (this.ws === undefined ) {
this.open();
}
return this.ws?.asObservable();
}
close() {
this.ws.unsubscribe();
}
}
In app.ts on nogOnit() :
if(this.auth.user()){
this.socketService.open()}