Quick Reference for AI Agents & Developers
- Connect:
CometChat.connect() - Disconnect:
CometChat.disconnect() - Disable auto-connect:
AppSettings.AppSettingsBuilder().autoEstablishSocketConnection(false).build() - Use case: Manual control for battery optimization or specific app flows
- Related: Connection Status · Connection Behaviour · Setup
Default SDK behaviour on login
When the login method of the SDK is called, the SDK performs the below operations:- Logs the user into the SDK
- Saves the details of the logged in user locally.
- Creates a web-socket connection for the logged in user.
Managing the Web-socket connections manually
The CometChat SDK also allows you to modify the above default behaviour of the SDK and take the control of the web-socket connection into your own hands. In order to achieve this, you need to follow the below steps:- While calling the init() function on the app startup, you need to inform the SDK that you will be managing the web socket connect. You can do so by using the
autoEstablishSocketConnection()method provided by theAppSettingsBuilderclass. This method takes a boolean value as an input. If set totrue, the SDK will manage the web-socket connection internally based on the default behaviour mentioned above. If set tofalse, the web socket connection can will not be managed by the SDK and you will have to handle it manually. You can refer to the below code snippet for the same:
- Swift
- Objective C
Sample Payload - Enable Manual WebSocket Mode
Sample Payload - Enable Manual WebSocket Mode
AppSettings Configuration:
Effect of Manual Mode:
Success Response:
Error Response:
| Parameter | Type | Description |
|---|---|---|
| setRegion(region:) | String | CometChat region. Example: "us" |
| autoEstablishSocketConnection() | Bool | Set to false to enable manual mode. Example: false |
| Setting | Behavior |
|---|---|
true (default) | SDK manages WebSocket automatically |
false | You must call connect() and disconnect() manually |
| Parameter | Type | Description |
|---|---|---|
| Success | Bool | Initialization success. Example: true |
| Parameter | Type | Description |
|---|---|---|
| errorCode | String | Error code. Example: "ERR_INVALID_APP_ID" |
| errorDescription | String | Error message. Example: "Invalid App ID" |
- You can manage the connection to the web-socket server using the
connect()anddisconnect()methods provided by the SDK. - Connect to the web-socket server
connect() method provided by the CometChat class to establish the connection to the web-socket server. Please make sure that the user is logged in to the SDK before calling this method. You can use the CometChat.getLoggedInUser() method to check this. Once the connection is established, you will start receiving all the real-time events for the logged in user
- Swift
- Objective C
Sample Payload - Connect to WebSocket
Sample Payload - Connect to WebSocket
Prerequisites:
Success Response:
After Connect:
Error Response:
| Requirement | Description |
|---|---|
| User logged in | CometChat.getLoggedInUser() != nil |
| Manual mode | autoEstablishSocketConnection(false) set in AppSettings |
| Parameter | Type | Description |
|---|---|---|
| Callback | Void | Success callback invoked when WebSocket connection established |
| Effect | Description |
|---|---|
| Real-time events | Start receiving messages, typing indicators, presence updates |
| Connection status | CometChat.getConnectionStatus?.value returns "connected" |
| Parameter | Type | Description |
|---|---|---|
| errorCode | String | Error code. Example: "ERROR_WEBSOCKETS_ALLREADY_IN_CONNECTED_STATE" |
| errorDescription | String | Error message. Example: "Web sockets connect called while web sockets are already connected" |
- Disconnect from the web-socket server
disconnect() method provided by the CometChat class to break the established connection. Once the connection is broken, you will stop receiving all the real-time events for the logged in user.
- Swift
- Objective C
Sample Payload - Disconnect from WebSocket
Sample Payload - Disconnect from WebSocket
Effect:
Success Response:
After Disconnect:
| Action | Description |
|---|---|
| WebSocket closed | Connection to server terminated |
| Real-time events | Stop receiving messages, typing indicators, presence updates |
| Connection status | CometChat.getConnectionStatus?.value returns "disconnected" |
| Parameter | Type | Description |
|---|---|---|
| Callback | Void | Success callback invoked when WebSocket disconnected |
| Effect | Description |
|---|---|
| Status | WebSocket disconnected |
| Events | No real-time events received |
| Action required | Call CometChat.connect() to reconnect |