Skip to main content
// Auto mode (default) - SDK manages connection automatically
val appSettings = AppSettings.AppSettingsBuilder()
    .setRegion("REGION")
    .autoEstablishSocketConnection(true)  // default
    .build()

// Manual mode - You control connection
val appSettings = AppSettings.AppSettingsBuilder()
    .setRegion("REGION")
    .autoEstablishSocketConnection(false)
    .build()

// Manual connection management
CometChat.connect(callback)  // Establish connection
CometChat.disconnect(callback)  // Break connection
CometChat.ping(callback)  // Keep alive (call within 30 seconds)
Default: Auto mode - SDK handles connection automatically
Manual mode: Requires explicit connect/disconnect calls

Default SDK Behaviour on Login

On login, the SDK logs the user in, saves their details locally, and creates a WebSocket connection. When the app is reopened and init() is called, the WebSocket reconnects automatically.

Auto Mode

The default mode. The SDK automatically establishes and maintains the WebSocket connection. Set autoEstablishSocketConnection(true) (or omit it — auto mode is the default).
App StateBehaviour
App in foregroundConnected with WebSocket
App in backgroundImmediately disconnected with WebSocket

Reconnection

If the app is in the foreground and there is no internet connection, the SDK will handle the reconnection of the WebSocket in auto mode.

Manual Mode

Set autoEstablishSocketConnection(false) to take control of the WebSocket connection. Call CometChat.connect() to establish and CometChat.disconnect() to break it. By default in manual mode, the SDK disconnects after 30 seconds in the background if no pings are received. Call CometChat.ping() within 30 seconds to keep the connection alive.
App StateBehaviour
App in foregroundCall CometChat.connect() to create the WebSocket connection
App in backgroundDisconnect the WebSocket connection if no ping is received within 30 seconds after the app goes in the background

Enable Manual Mode

Set autoEstablishSocketConnection(false) during init() to take control of the WebSocket connection. Error callbacks receive a CometChatException:
String appId = "YOUR_APP_ID";
String region = "us";

AppSettings appSettings = new AppSettings.AppSettingsBuilder()
  .setRegion(region)
  .autoEstablishSocketConnection(false) //set it as false for manual mode
  .build();

CometChat.init(this, appId, appSettings, new CometChat.CallbackListener<String>() {
  @Override
  public void onSuccess(String s) {
      Log.d(TAG, "Init successful!");
  }

  @Override
  public void onError(CometChatException e) {
      Log.d(TAG, "Error occurred : " + e.getMessage());
  }
});
You can manage the connection to the WebSocket server using the connect(), disconnect(), and ping() methods provided by the SDK.

Connect to the WebSocket Server

Call connect() to establish the connection. Ensure the user is logged in first (CometChat.getLoggedInUser()).
CometChat.connect(new CometChat.CallbackListener<String>() {
  @Override
  public void onSuccess(String s) {
      
  }

  @Override
  public void onError(CometChatException e) {

  }
});

Disconnect from the WebSocket Server

Call disconnect() to break the connection. Real-time events stop until reconnected.
CometChat.disconnect(new CometChat.CallbackListener<String>() {
  @Override
  public void onSuccess(String s) {
      
  }

  @Override
  public void onError(CometChatException e) {

  }
});

Maintain Long-Standing Background Connection

Call CometChat.ping() within 30 seconds of the app entering the background to keep the connection alive.
CometChat.ping(new CometChat.CallbackListener<String>() {
  @Override
  public void onSuccess(String s) {

  }

  @Override
  public void onError(CometChatException e) {

  }
});
In manual mode with the app in the foreground, the SDK auto-reconnects if the internet drops. If the app is in the background and the connection was disconnected, call CometChat.connect() to reconnect.

Next Steps

Setup

Configure SDK initialization settings

Connection Status

Monitor SDK connection status changes

Login Listeners

Handle user authentication events

Real-Time Listeners

Receive real-time messages and events