Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda