Skip to main content
Quick Reference for AI Agents & Developers
// Add connection listener
CometChat.addConnectionListener("LISTENER_ID", object : ConnectionListener {
    override fun onConnected() { /* Connection established */ }
    override fun onConnecting() { /* Attempting to connect */ }
    override fun onDisconnected() { /* Connection lost */ }
    override fun onFeatureThrottled() { /* Features throttled */ }
    override fun onError(e: CometChatException) { /* Connection error */ }
})

// Get current connection status
val status = CometChat.getConnectionStatus()
// Returns: WS_STATE_CONNECTED, WS_STATE_CONNECTING, WS_STATE_DISCONNECTED, or WS_STATE_FEATURE_THROTTLED

// Remove listener
CometChat.removeConnectionListener("LISTENER_ID")
CometChat SDK provides you with a mechanism to get real-time status of the connection to CometChat web-socket servers. To achieve this you need to use the ConnectionListener class provided by the CometChat SDK Connection Status provides you with the below 3 methods to get the status of the connection to CometChat web-socket servers:
Delegate MethodInformation
onConnectingThis method is triggered when CometChat SDK is trying to establish a connection to the web-socket server.
onConnectedThis method is called when CometChat SDK has successfully established a connection and now is connected.
onDisconnectedThis method is called when the CometChat SDK gets disconnected due to any issue while maintaining the connection like network fluctuations, etc.
onFeatureThrottledCometChat automatically toggles off certain features to prevent performance loss for end-users under various circumstances
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. In order to use the ConnectionListeners, you need to add the ConnectionListeners using the addConnectionListener method provided by the SDK. You can add multiple listeners as shown below. Just make sure you add listeners with unique IDs.
CometChat.addConnectionListener("UNIQUE_LISTENER_ID", new CometChat.ConnectionListener() {
  @Override
    public void onConnected() {
    Log.i(TAG, "onConnected: ");
  }
  
  @Override
    public void onConnecting() {
    Log.i(TAG, "onConnecting: ");
  }

  @Override
    public void onDisconnected() {
    Log.i(TAG, "onDiconnected: ");
  }
  
  @Override
    public void onFeatureThrottled() {
    Log.i(TAG, "onFeatureThrottled: ");
  }
  
  @Override
    public void onConnectionError(CometChatException e) {
    Log.i(TAG, "onConnectionError: ");
  }
});
You can also get the current connection status by using getConnectionStatus property provided by CometChat SDK
String connectionStatus = CometChat.getConnectionStatus();  
The above method will return either of the below 3 values:
  1. CometChatConstants.WS_STATE_CONNECTED (connected);
  2. CometChatConstants.WS_STATE_CONNECTING(connecting)
  3. CometChatConstants.WS_STATE_DISCONNECTED(disconnected)
  4. CometChatConstants.WS_STATE_FEATURE_THROTTLED(featureThrottled)
Know more about CometChat SDK connection behaviour click here

Best Practices

Display connection status indicators in your UI (e.g., “Connecting…”, “Connected”, “Offline”) to inform users about network issues. Update the UI based on connection listener callbacks.
The SDK automatically attempts to reconnect when disconnected. Show a reconnecting indicator and avoid blocking the UI. Don’t manually call login() again during reconnection.
Queue user actions (sending messages, making calls) when disconnected and execute them once the connection is restored. This provides a better user experience during network fluctuations.
Remove connection listeners in onDestroy() to prevent memory leaks. Use unique listener IDs to manage multiple listeners across different activities.

Next Steps