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.
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:
- JavaScript
init() when a session exists, and handles reconnection on network drops. This page covers how to disable that and manage the connection yourself.
You’d want manual control when you need to conserve resources by connecting only when the user is actively chatting, or when you need precise control over when real-time events start flowing.
Default Behavior
WhenautoEstablishSocketConnection is true (the default):
CometChat.login()logs the user in, saves their session locally, and opens a WebSocket connection- On app restart,
CometChat.init()automatically reconnects using the saved session - The user immediately starts receiving real-time messages, presence updates, and call events
Manual Connection Management
To take control of the WebSocket connection, setautoEstablishSocketConnection(false) during initialization:
- TypeScript
- JavaScript
connect() and disconnect() to control the WebSocket connection.
Connect
Establishes the WebSocket connection. The user must be logged in first (check withCometChat.getLoggedinUser()). Once connected, real-time events start flowing. Returns void.
- Connect
Disconnect
Breaks the WebSocket connection. Real-time events stop until you callconnect() again. Returns void.
- Disconnect
Next Steps
Connection Status
Monitor the SDK connection state in real time
Login Listener
Listen for login and logout events
All Real-Time Listeners
Complete reference for all SDK event listeners
Setup SDK
SDK installation and initialization guide