Skip to main content
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:
  1. Logs the user into the SDK
  2. Saves the details of the logged in user locally.
  3. Creates a web-socket connection for the logged in user.
This makes sure that the logged in user starts receiving real-time messages sent to him or any groups that he is a part of as soon as he logs in. When the app is reopened, and the init() method is called, the web-socket connection to the server is established automatically. This is the default behaviour of the CometChat SDKs. However, if you wish to take control of the web-socket connection i.e if you wish to connect and disconnect to the web-socket server manually, you can refer to the Managing Web-socket Connection section.

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:
  1. 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 the AppSettingsBuilder class. This method takes a boolean value as an input. If set to true , the SDK will manage the web-socket connection internally based on the default behaviour mentioned above. If set to false , 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:
let appSettings = AppSettings.AppSettingsBuilder()
									.setRegion(region: "us")
									.autoEstablishSocketConnection(false)
									.build()
        
let _ =  CometChat.init(appId:"1976246d33493296",
                        appSettings: appSettings,
                        onSuccess: { (Success) in
            print( "Initialization onSuccess \(Success)")
        }) { (error) in
            print( "Initialization Error Code:  \(error.errorCode)")
            print( "Initialization Error Description:  \(error.errorDescription)")
        }
AppSettings Configuration:
ParameterTypeDescription
setRegion(region:)StringCometChat region. Example: "us"
autoEstablishSocketConnection()BoolSet to false to enable manual mode. Example: false
Effect of Manual Mode:
SettingBehavior
true (default)SDK manages WebSocket automatically
falseYou must call connect() and disconnect() manually
Success Response:
ParameterTypeDescription
SuccessBoolInitialization success. Example: true
Error Response:
ParameterTypeDescription
errorCodeStringError code. Example: "ERR_INVALID_APP_ID"
errorDescriptionStringError message. Example: "Invalid App ID"
  1. You can manage the connection to the web-socket server using the connect() and disconnect() methods provided by the SDK.
  2. Connect to the web-socket server
You need to use the 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
CometChat.connect()
Prerequisites:
RequirementDescription
User logged inCometChat.getLoggedInUser() != nil
Manual modeautoEstablishSocketConnection(false) set in AppSettings
Success Response:
ParameterTypeDescription
CallbackVoidSuccess callback invoked when WebSocket connection established
After Connect:
EffectDescription
Real-time eventsStart receiving messages, typing indicators, presence updates
Connection statusCometChat.getConnectionStatus?.value returns "connected"
Error Response:
ParameterTypeDescription
errorCodeStringError code. Example: "ERROR_WEBSOCKETS_ALLREADY_IN_CONNECTED_STATE"
errorDescriptionStringError message. Example: "Web sockets connect called while web sockets are already connected"
  1. Disconnect from the web-socket server
You can use the 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.
CometChat.disconnect()
Effect:
ActionDescription
WebSocket closedConnection to server terminated
Real-time eventsStop receiving messages, typing indicators, presence updates
Connection statusCometChat.getConnectionStatus?.value returns "disconnected"
Success Response:
ParameterTypeDescription
CallbackVoidSuccess callback invoked when WebSocket disconnected
After Disconnect:
EffectDescription
StatusWebSocket disconnected
EventsNo real-time events received
Action requiredCall CometChat.connect() to reconnect