Skip to main content

Documentation Index

Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

// Add connection listener
CometChat.addConnectionListener("connection_listener", ConnectionListenerImpl());

// Connection listener implementation
class ConnectionListenerImpl with ConnectionListener {
  @override
  void onConnected() => debugPrint("Connected");
  
  @override
  void onConnecting() => debugPrint("Connecting...");
  
  @override
  void onDisconnected() => debugPrint("Disconnected");
  
  @override
  void onFeatureThrottled() => debugPrint("Feature throttled");
  
  @override
  void onConnectionError(CometChatException error) => debugPrint("Error: ${error.message}");
}

// Check current connection status
String status = CometChat.getConnectionStatus();
// Returns: CometChatWSState.connected, connecting, disconnected, or featureThrottled

// Remove listener when done
CometChat.removeConnectionListener("connection_listener");
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
CometChatWSState.connectedonConnected()SDK has an active connection to CometChat servers
CometChatWSState.connectingonConnecting()SDK is attempting to establish or re-establish a connection
CometChatWSState.disconnectedonDisconnected()SDK is disconnected due to network issues or other errors
CometChatWSState.featureThrottledonFeatureThrottled()A feature has been throttled to prevent performance loss
onConnectionError(CometChatException)An error occurred while maintaining the connection

Get Current Status

Use getConnectionStatus() to check the current connection state at any time:
String connectionStatus = CometChat.getConnectionStatus();
The method returns one of the following values:
  1. CometChatWSState.connected (connected)
  2. CometChatWSState.connecting (connecting)
  3. CometChatWSState.disconnected (disconnected)
  4. CometChatWSState.featureThrottled (featureThrottled)

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.

ConnectionListener Events

EventParameterDescription
onConnected()Triggered when the SDK successfully establishes a connection to the WebSocket server
onConnecting()Triggered when the SDK is attempting to establish a connection to the WebSocket server
onDisconnected()Triggered when the SDK gets disconnected due to network fluctuations or other issues
onFeatureThrottled()Triggered when CometChat automatically toggles off certain features to prevent performance loss
onConnectionError(CometChatException error)CometChatExceptionTriggered when an error occurs while maintaining the WebSocket connection
class Class_Name  with ConnectionListener {
//1. Register Connection listener
//CometChat.addConnctionListener("listenerId", this);

//2. Ovveride the ConnectionListener methods
@override
void onConnected() {
  // TODO: implement onConnected
}

@override
void onConnecting() {
  // TODO: implement onConnecting
}

@override
void onDisconnected() {
  // TODO: implement onDisconnected
}

@override
void onFeatureThrottled() {
  // TODO: implement onFeatureThrottled
}

@override
void onConnectionError(CometChatException error) {
  // TODO: implement onFeatureThrottled
}

}
Always remove connection listeners when they’re no longer needed (e.g., on widget dispose or navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.

Remove Connection Listener

CometChat.removeConnectionListener("listenerId");
Know more about CometChat SDK connection behaviour click here

Next Steps

Connection Behaviour

Understand how CometChat SDK manages WebSocket connections

Login Listeners

Monitor user login and logout events in real-time

All Real-Time Listeners

Complete reference for all SDK listeners

Setup SDK

Install and initialize the CometChat SDK