Skip to main content
Quick Reference for AI Agents & Developers
  • Add listener: CometChat.addConnectionListener("UNIQUE_ID", self as CometChatConnectionDelegate)
  • Remove listener: CometChat.removeConnectionListener("UNIQUE_ID")
  • Delegate methods: connecting(), connected(), disconnected()
  • Related: Setup · WebSocket Connection · Overview
CometChat SDK provides you with a mechanism to get real-time status of the connection to CometChat web-socket servers. This can be achieved by registering for the events using the CometChatConnectionDelegate class. Connection Status provides you with the below 3 methods to get the status of the connection to CometChat web-socket servers:
Delegate MethodInformation
connectingThis method is triggered when CometChat SDK is trying to establish a connection to the web-socket server.
connectedThis method is called when CometChat SDK has successfully established a connection and now is connected.
disconnectedThis method is called when the CometChat SDK gets disconnected due to any issue while maintaining the connection like network fluctuations, etc.
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 Delegate methods you must add protocol conformance CometChatConnectionDelegate as CometChat.conectiondelegate = self . Here is the example of CometChatConnectionDelegate:
extension AppDelegate: CometChatConnectionDelegate {

	func connecting() {
    
  	print("connecting")
  }
    
  func connected() {
        
    print("connected")  
  }
    
  func disconnected() {
  
  	print("disconnected")
  }
}
connecting() Event:
ParameterTypeDescription
(none)VoidNo parameters passed to this callback
Event Trigger: SDK is attempting to establish WebSocket connection
ScenarioDescription
Initial connectionAfter login, SDK tries to connect
ReconnectionAfter disconnection, SDK auto-reconnects
Network recoveryWhen network becomes available again
connected() Event:
ParameterTypeDescription
(none)VoidNo parameters passed to this callback
Event Trigger: WebSocket connection successfully established
EffectDescription
Real-time eventsStart receiving messages, typing indicators, presence
StatusConnection is active and stable
disconnected() Event:
ParameterTypeDescription
(none)VoidNo parameters passed to this callback
Event Trigger: WebSocket connection lost
CauseDescription
Network issuesWiFi/cellular connection lost
Server issuesCometChat server temporarily unavailable
Manual disconnectCometChat.disconnect() called
Connection Flow:
StepEventDescription
1disconnected()Connection breaks
2connecting()SDK auto-reconnects
3connected()Reconnection successful
We recommend you to add the Connection Status delegate in your AppDelegate and in your app’s first view controller that opens when you log in.
You can also get the current connection status by using getConnectionStatus property provided by CometChat SDK
var connectionStatus = CometChat.getConnectionStatus?.value
Method:
MethodReturn TypeDescription
CometChat.getConnectionStatus?.valueString?Current WebSocket connection status
Response:
ParameterTypeDescription
connectionStatusString?Current status. Example: "disconnected"
Possible Values:
ValueDescription
"connecting"SDK is trying to establish WebSocket connection
"connected"Successfully connected to WebSocket server
"disconnected"Not connected to WebSocket server
The CometChat.getConnectionStatus method will return either of the below 3 values:
  1. connecting
  2. connected
  3. disconnected