Answer the question
In order to leave comments, you need to log in
How to remove a specific socket.io listener on the client?
Socket (on the client) is initialized on multiple pages:
What do I need to write in ngOnDestroy(){} or in ionViewDidLeave() {} to unsubscribe the listener only on this page I'm leaving?
I'll describe the problem in detail: There is a page, request.page.ts , which is used multiple times. The data displayed on it depends on the id, for example: "/home/request;id=24" and "/home/request;id=175".
When leaving the page, I need to destroy it. I'm trying to do this in several ways:
this.socket.on('server-response', data => { })
goBack(){
this.navCtrl.navigateRoot('/home');
this.router.navigate(['/home'], {replaceUrl: true})
this.location.back();
}
this.socket.removeListener('server-response');
Answer the question
In order to leave comments, you need to log in
Chet you have porridge.
Subscribing to a socket is not a matter of a specific page, it is a task for logic at the level of the entire application. If the business task is such that you need to open a subscription when you open the page, then the page should notify the service about its activity.
Let's say I'm leaving the page "/home/request;id=24". After such an exit, the history of url-movements is, as it were, deleted, I cannot return to it by clicking on the "back" button
Socket (on the client) is initialized in multiple pages:
this.socket.on('server-response', data => { })
serverResponse = new Observable(observer => {
this.socket.on('server-response', data => ovserver.next(data));
return () => this.socket.removeListener('server-response');
}).pipe(share());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question