Skip to main content
// 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")
Use ConnectionListener to monitor real-time WebSocket connection state. The SDK automatically attempts to reconnect when disconnected.
CallbackDescription
onConnectingSDK is attempting to establish a WebSocket connection
onConnectedConnection successfully established
onDisconnectedConnection lost (network fluctuation, etc.)
onFeatureThrottledCometChat toggled off certain features to prevent performance loss
Error callbacks receive a CometChatException with details about the connection failure.
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: ");
  }
});

Get Current Status

Use getConnectionStatus() to check the current connection state at any time:
String connectionStatus = CometChat.getConnectionStatus();  
Returns one of:
ValueDescription
CometChatConstants.WS_STATE_CONNECTEDActive connection
CometChatConstants.WS_STATE_CONNECTINGAttempting to connect
CometChatConstants.WS_STATE_DISCONNECTEDNo connection
CometChatConstants.WS_STATE_FEATURE_THROTTLEDFeature throttled
Know more about CometChat SDK connection behaviour click here

Next Steps

Connection Behaviour

Learn about SDK connection behavior and reconnection logic

Setup

Initialize the SDK and establish connection

User Presence

Track user online/offline status

Real-Time Listeners

Register listeners for messages, users, groups, and calls