Skip to main content
// Get current status: "connecting" | "connected" | "disconnected"
const status = CometChat.getConnectionStatus();

// Listen for connection changes
CometChat.addConnectionListener("LISTENER_ID", new CometChat.ConnectionListener({
  onConnected: () => console.log("Connected"),
  inConnecting: () => console.log("Connecting..."),
  onDisconnected: () => console.log("Disconnected")
}));

// Cleanup
CometChat.removeConnectionListener("LISTENER_ID");
CometChat SDK provides you with a mechanism to get real-time status of the connection to CometChat web-socket servers. Connection Status provides you with the below 3 methods to get the status of the connection to CometChat web-socket servers:
Delegate MethodInformation
connectingThis method is triggered when CometChat SDK is trying to establish a connection to the web-socket server.
connectedThis method is called when CometChat SDK has successfully established a connection and now is connected.
disconnectedThis method is called when the CometChat SDK gets disconnected due to any issue while maintaining the connection like network fluctuations, etc.
Once the connection is broken, the disconnected callback is triggered, the SDK automatically tries to establish the connection again, thus going into the connecting state and triggering the connecting method. Once the attempt to connect is successful, the connected method is triggered thus letting the developer know that the connection is established and is active. To receive real-time connection status, you need to register ConnectionListener wherever you wish to receive the real-time status. You can use the addConnectionListener() method to do so.
let listenerID = "UNIQUE_LISTENER_ID";
CometChat.addConnectionListener(
  listenerID,
  new CometChat.ConnectionListener({
    onConnected: () => {
      console.log("ConnectionListener => On Connected");
    },
    inConnecting: () => {
      console.log("ConnectionListener => In connecting");
    },
    onDisconnected: () => {
      console.log("ConnectionListener => On Disconnected");
    },
  })
);
We recommend you to add the Connection Listener in your method on app startup, preferably in the index.js file. Once you have successfully initialized CometChat.
You can also get the current connection status by using getConnectionStatus property provided by CometChat SDK
const connectionStatus = CometChat.getConnectionStatus();
The CometChat.getConnectionStatus method will return either of the below 3 values:
  1. connecting
  2. connected
  3. disconnected
The connection listener callbacks and getConnectionStatus() return string enum values representing the current WebSocket connection state:
ValueCallbackDescription
"connected"onConnected()SDK has an active connection to CometChat servers
"connecting"inConnecting()SDK is attempting to establish or re-establish a connection
"disconnected"onDisconnected()SDK is disconnected due to network issues or other errors
"featureThrottled"A feature has been throttled due to rate limiting
Always remove connection listeners when they’re no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.

Next Steps

WebSocket Management

Manually manage WebSocket connections

Login Listener

Listen for login and logout events

All Real-Time Listeners

Complete reference for all SDK listeners

Setup SDK

Install and initialize the CometChat SDK