This is how I establish a connection between the client and server using socket io.
export class OverviewService {
socket:any;
readonly uri:string='http://localhost:4000';
constructor(private http: HttpClient) {
this.socket = io(this.uri);
}
And here is how I make the API call:
getOnPeak(){
this.socket.emit('on_peak/today');
return new Observable((subscriber)=>{
this.socket.on('on_peak/today',(data:any)=>{
subscriber.next(data.onPeak.toFixed(2))
console.log('onPeak:'+data.onPeak.toFixed(2));
})
})
}
In overview.ts file:
this.getOnPeekSub=this.overviewService.getOnPeak().subscribe(onPeak => {
this.onPeak = onPeak;
})
Upon ngOnDestroy, I attempted to use removeAllListeners(), removeListeners() and off() but encountered issues with them not functioning as expected.