Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
How to unsubscribe socket io in Angular

This how I connect client to server with socket io.

export class OverviewService {

  socket:any;
  readonly uri:string='http://localhost:4000';

  

  constructor(private http: HttpClient) {
    this.socket = io(this.uri);
   }

and this when I 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));
      })
    })
    
  }

overview.ts

this.getOnPeekSub=this.overviewService.getOnPeak().subscribe(onPeak => {
      this.onPeak = onPeak;
    })

on ngOnDestroy i tried to use removeAllListeners(),removeListeners() and off() but it doesn't work .

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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));
      })

      return function unsubscribe() {
        this.socket.disconnect()
      };
    })
    
  }

When you unsubscribe from the Observalbe, the socket will be closed. Read more here https://rxjs.dev/guide/observable (Disposing Observable Executions)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda