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");
The CometChat SDK maintains a WebSocket connection to CometChat servers for real-time events. You can check the current connection state and listen for changes — useful for showing connectivity indicators in your UI or queuing operations while offline. When the connection drops, the SDK automatically attempts to reconnect, cycling through disconnectedconnectingconnected.

Connection States

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

Get Current Status

Use getConnectionStatus() to check the current connection state at any time:
const connectionStatus: string = CometChat.getConnectionStatus();

Listen for Connection Changes

Register a ConnectionListener to receive real-time connection state updates. We recommend adding this on app startup after CometChat.init() completes.
let listenerID: string = "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");
    },
  })
);
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