ConnectionListener shows "connected" when there is no network connection

ConnectionListener says “connected” when there is no network connection in the browser. How is that possible?

1 Like

Hello @ebrake,

Can you please share code snippet and screen capture for us to understand the issue better?

Hi @mayur.bhandari

Here are the steps I have used with Chrome which should work, let me know if you still need a screen share:

Step 1, initialize CometChat and add a connection listener

const listenerID = Math.random().toString(36).substring(2) + Date.now().toString(36)
CometChat.addConnectionListener(
  listenerID,
  new CometChat.ConnectionListener({
    onConnected: () => {
      console.log('connected')
    },
    inConnecting: () => {
      console.log('connecting')
    },
    onDisconnected: () => {
      console.log('disconnected')
    }
  })
)

Step 2, load the page in your browser, it should go from ‘connecting’ to ‘connected’ as the successful connection state.

Step 3, disconnect from your network on your computer (e.g. turn off Wifi)

  • onDisconnected event is not fired, ‘connected’ continues to be the connection listener state

  • CometChat.getConnectionStatus() continues to say “connected”

Step 4, connect to the Internet again, the connection listener does not reconnect, is still in “connected” state.

My expectation is that CometChat would send a disconnect event if the network connection becomes broken, and attempt to reconnect when the connection becomes available.

@mayur.bhandari Were you able to reproduce? I am finding that if there is a chat connection issue, the user has no way of knowing currently, and no new messages get delivered.

Hello @ebrake,

I was not able to reproduce this issue. I tried it on macOS chrome (v81.0.4044.138). I used JS SDK v2.0.7. I was connected on wifi and logged-in then on turning the wifi off i got onDisconnected and on reconnecting to the wifi i got onConnected.

Please have a look at the screen recording shared below.

1 Like

Hi @mayur.bhandari I have followed your example, with the simplest demo. I have confirmed that the disconnect event does get fired, but here, it can take 20 seconds for the event to occur, perhaps I wasn’t waiting long enough before. It is a long time to get a disconnect event, wondering if there is a way to get a more immediate callback.

1 Like

Hello @ebrake,

Currently the connection status detection logic works on the basis of a ping mechanism. The ping is configured at an interval of 25 seconds. So when you disconnect the wifi, it will be detected when the next ping is executed(after a maximum of 25 seconds). I hope this helps.

1 Like

Aha! Thank you @mayur.bhandari this is helpful information.

2 Likes