Skip to main content
Quick Reference for AI Agents & Developers
// Disable auto-connect during init
const appSettings = new CometChat.AppSettingsBuilder()
  .setRegion("REGION")
  .autoEstablishSocketConnection(false)  // Manual mode
  .build();

// Manual connect/disconnect
CometChat.connect();     // Connect WebSocket
CometChat.disconnect();  // Disconnect WebSocket
By default, CometChat SDK manages WebSocket connections automatically. For advanced use cases, you can take manual control of the connection.
Default Behavior: The SDK automatically connects to WebSocket servers on login and reconnects when the app reopens.

Default vs Manual Mode

AspectDefault (Auto)Manual
Connection on loginAutomaticYou control
ReconnectionAutomaticYou control
Use caseMost appsBattery optimization, background control

Enable Manual Mode

Disable automatic WebSocket management during initialization:
const appID = "APP_ID";
const region = "APP_REGION";

const appSettings = new CometChat.AppSettingsBuilder()
  .subscribePresenceForAllUsers()
  .setRegion(region)
  .autoEstablishSocketConnection(false)  // Disable auto-connect
  .build();

CometChat.init(appID, appSettings).then(
  () => console.log("Initialized (manual WebSocket mode)"),
  (error) => console.log("Init failed:", error)
);

Connect to WebSocket

Manually establish the WebSocket connection:
// Ensure user is logged in first
const user = await CometChat.getLoggedinUser();

if (user) {
  CometChat.connect();
  console.log("WebSocket connection initiated");
}
Only call connect() after the user is logged in. Use CometChat.getLoggedinUser() to verify.

Disconnect from WebSocket

Manually close the WebSocket connection:
CometChat.disconnect();
console.log("WebSocket disconnected");

Use Cases

Disconnect when the app goes to background to save battery, reconnect when foregrounded.
Only connect to real-time when user opens chat, disconnect when they leave.
Control connections based on network type (WiFi vs cellular).

Implementation Example

class WebSocketManager {
  constructor() {
    this.isConnected = false;
  }

  async connect() {
    const user = await CometChat.getLoggedinUser();
    if (!user) {
      console.log("User not logged in");
      return false;
    }

    CometChat.connect();
    this.isConnected = true;
    console.log("WebSocket connected");
    return true;
  }

  disconnect() {
    CometChat.disconnect();
    this.isConnected = false;
    console.log("WebSocket disconnected");
  }

  // Call when app goes to background
  onAppBackground() {
    if (this.isConnected) {
      this.disconnect();
    }
  }

  // Call when app comes to foreground
  async onAppForeground() {
    if (!this.isConnected) {
      await this.connect();
    }
  }
}

// Usage
const wsManager = new WebSocketManager();

// When user opens chat
await wsManager.connect();

// When app goes to background
document.addEventListener("visibilitychange", () => {
  if (document.hidden) {
    wsManager.onAppBackground();
  } else {
    wsManager.onAppForeground();
  }
});

Next Steps