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

195
Visualizações
How to keep the connection with Socket.IO?

I have a taxi app which connect through socket.io in order to receive events from driver side like location and responses.

The issue I face is that if I put the client app in background it will get disconnected after a while and I get the following messages in terminal

 ID context fcRzOWmBSQb3RWlUAAXQ
 LOG  DISCONNECTED ping timeout
 LOG  Is disconnected ping timeout
 LOG  DISCONNECTED ping timeout
 LOG  DISCONNECTED ping timeout
 LOG  Is disconnected ping timeout
 LOG  DISCONNECTED ping timeout
 LOG  Is disconnected ping timeout
 LOG  DISCONNECTED ping timeout
 LOG  Is disconnected ping timeout
 LOG  ID context reconnected undefined
 LOG  ID context reconnected undefined
 LOG  ID context reconnected BhjFYCZXf7BE8vEhAAXw
 LOG  ID context reconnected BhjFYCZXf7BE8vEhAAXw
 LOG  ID context BhjFYCZXf7BE8vEhAAXw
 LOG  ID context BhjFYCZXf7BE8vEhAAXw
 LOG  ID context BhjFYCZXf7BE8vEhAAXw
 LOG  ID context reconnected BhjFYCZXf7BE8vEhAAXw
 LOG  ID context BhjFYCZXf7BE8vEhAAXw

here is my SocketClient.js

export default class socketAPI {
  socket;

  connect(user, listener) {
    this.socket = io(BASE_URL, {
      query: `type=client&id=${user}`,
      reconnection: true,
      transports: ['websocket', 'polling'],
    });

    this.socket.on('connect', () => {
      listener(true);
    });

    this.socket.on('disconnect', reason => {
      console.log('DISCONNECTED', reason);
    });
  }

  disconnect(listener) {
    this.socket.disconnect(reason => {
      this.socket = null;
      listener(false);
    });
  }

  emit(event, data, listener) {
    if (!this.socket) {
      console.log('No socket connection. Emit event');
      return null;
    }

    this.socket.emit(event, data, listener);
  }

  on(event, listener) {
    if (!this.socket) {
      console.log('No socket connection. On event');
      return null;
    }

    this.socket.on(event, listener);
  }

  off(event, listener) {
    if (!this.socket) {
      console.log('No socket connection. On event');
      return null;
    }

    this.socket.off(event, listener);
  }
}

And this is the part from SocketContext.js where I make the connection

  useEffect(() => {
    socketClient.connect(client.id, () => {
      console.log('ID context', socketClient.socket.id);
      dispatch(updateSocketStatus(socketClient.socket.id));
    });
    socketClient.on('disconnect', isDisconnected => {
      console.log('Is disconnected', isDisconnected);
      dispatch(updateSocketStatus(null));
      if (isDisconnected) {
        socketClient.connect(client.id, () => {
          console.log('ID context reconnected', socketClient.socket.id);
          dispatch(updateSocketStatus(socketClient.socket.id));
        });
      }
    });
  }, [client]);
about 4 years ago · Juan Pablo Isaza
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