Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!