Quick Reference for AI Agents & Developers
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 Method | Information |
|---|---|
| onConnecting | This method is triggered when CometChat SDK is trying to establish a connection to the web-socket server. |
| onConnected | This method is called when CometChat SDK has successfully established a connection and now is connected. |
| onDisconnected | This method is called when the CometChat SDK gets disconnected due to any issue while maintaining the connection like network fluctuations, etc. |
| onFeatureThrottled | CometChat automatically toggles off certain features to prevent performance loss for end-users under various circumstances |
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.
- Java
- Kotlin
getConnectionStatus property provided by CometChat SDK
- Java
- Kotlin
-
CometChatConstants.WS_STATE_CONNECTED(connected); -
CometChatConstants.WS_STATE_CONNECTING(connecting) -
CometChatConstants.WS_STATE_DISCONNECTED(disconnected) -
CometChatConstants.WS_STATE_FEATURE_THROTTLED(featureThrottled)
Know more about CometChat SDK connection behaviour click here
Best Practices
Monitor Connection Status in UI
Monitor Connection Status in UI
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.
Handle Reconnection Gracefully
Handle Reconnection Gracefully
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 Actions During Disconnection
Queue Actions During Disconnection
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
Remove Connection Listeners
Remove connection listeners in
onDestroy() to prevent memory leaks. Use unique listener IDs to manage multiple listeners across different activities.