Skip to main content
// Disable auto WebSocket connection during init
const appSettings = new CometChat.AppSettingsBuilder()
  .setRegion("REGION")
  .autoEstablishSocketConnection(false)
  .build();
await CometChat.init("APP_ID", appSettings);

// Manually connect/disconnect
CometChat.connect();
CometChat.disconnect();
By default, the SDK automatically establishes and manages the WebSocket connection — it connects on login, reconnects on 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

When autoEstablishSocketConnection is true (the default):
  1. CometChat.login() logs the user in, saves their session locally, and opens a WebSocket connection
  2. On app restart, CometChat.init() automatically reconnects using the saved session
  3. The user immediately starts receiving real-time messages, presence updates, and call events

Manual Connection Management

To take control of the WebSocket connection, set autoEstablishSocketConnection(false) during initialization:
let appID: string = "APP_ID";
let region: string = "APP_REGION";
let appSetting: CometChat.AppSettings = new CometChat.AppSettingsBuilder()
  .subscribePresenceForAllUsers()
  .setRegion(region)
  .autoEstablishSocketConnection(false)
  .build();
CometChat.init(appID, appSetting).then(
  (isInitialized: boolean) => {
    console.log("Initialization completed successfully");
  },
  (error: CometChat.CometChatException) => {
    console.log("Initialization failed with error:", error);
  }
);
Once initialized with manual mode, use connect() and disconnect() to control the WebSocket connection.

Connect

Establishes the WebSocket connection. The user must be logged in first (check with CometChat.getLoggedinUser()). Once connected, real-time events start flowing. Returns void.
CometChat.connect();

Disconnect

Breaks the WebSocket connection. Real-time events stop until you call connect() again. Returns void.
CometChat.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