> ## 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.

# Connection Behaviour

> Configure CometChat Android SDK WebSocket connection behavior for automatic or manual connection management in chat apps.

> [accordion] AI Integration Quick Reference
>
> ```kotlin theme={null}
> // 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).

![CometChat automatic WebSocket handling flow showing login, automatic connection, background disconnect, and auto reconnect on return to foreground](https://mintcdn.com/cometchat-22654f5b/vXo7PmP2iKdqqr3B/images/automatic-web-socket-handling.png?fit=max\&auto=format\&n=vXo7PmP2iKdqqr3B\&q=85\&s=56b6511850c5914263ed79352e6c46da)

| App State | Behaviour |
| - | - |
| App in foreground | Connected with WebSocket |
| App in background | Immediately 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.

![CometChat manual WebSocket handling flow showing manual connect, 30-second background timeout, ping keep alive, optional disconnect, and reconnect flow](https://mintcdn.com/cometchat-22654f5b/FKA-ESkxYmHmYrty/images/manual-web-socket-connection-handling.png?fit=max\&auto=format\&n=FKA-ESkxYmHmYrty\&q=85\&s=71daeb0bf3cf84cb49df62406727faa5)

| App State | Behaviour |
| - | - |
| App in foreground | Call `CometChat.connect()` to create the WebSocket connection |
| App in background | Disconnect 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`](https://www.cometchat.com/docs/sdk/reference/auxiliary#cometchatexception):

> [tab] Java
>
> ```java theme={null}
> 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());
>   }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> val appId = "YOUR_APP_ID"
> val region = "us"
>
> val appSettings = AppSettings.AppSettingsBuilder()
>   .setRegion(region)
>   .autoEstablishSocketConnection(false) // set to false for manual mode
>   .build()
>
> CometChat.init(this, appId, appSettings, object : CometChat.CallbackListener<String>() {
>   override fun onSuccess(s: String) {
>       Log.d(TAG, "Init successful!")
>   }
>
>   override fun onError(e: CometChatException) {
>       Log.d(TAG, "Error occurred : " + e.message)
>   }
> })
> ```

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()`).

> [tab] Java
>
> ```java theme={null}
> CometChat.connect(new CometChat.CallbackListener<String>() {
>   @Override
>   public void onSuccess(String s) {
>
>   }
>
>   @Override
>   public void onError(CometChatException e) {
>
>   }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> CometChat.connect(object : CallbackListener<String>() {
>   fun onSuccess(s: String) {
>
>   }
>
>   fun onError(e: CometChatException) {
>
>   }
> })
> ```

## Disconnect from the WebSocket Server

Call `disconnect()` to break the connection. Real-time events stop until reconnected.

> [tab] Java
>
> ```java theme={null}
> CometChat.disconnect(new CometChat.CallbackListener<String>() {
>   @Override
>   public void onSuccess(String s) {
>
>   }
>
>   @Override
>   public void onError(CometChatException e) {
>
>   }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> CometChat.disconnect(object : CallbackListener<String>() {
>   fun onSuccess(s: String) {
>
>   }
>
>   fun onError(e: CometChatException) {
>
>   }
> })
> ```

## Maintain Long-Standing Background Connection

Call `CometChat.ping()` within 30 seconds of the app entering the background to keep the connection alive.

> [tab] Java
>
> ```java theme={null}
> CometChat.ping(new CometChat.CallbackListener<String>() {
>   @Override
>   public void onSuccess(String s) {
>
>   }
>
>   @Override
>   public void onError(CometChatException e) {
>
>   }
> });
> ```

> [tab] Kotlin
>
> ```kotlin theme={null}
> CometChat.ping(object : CallbackListener<String>() {
>   override fun onSuccess(s: String) {
>
>   }
>
>   override fun onError(e: CometChatException) {
>
>   }
> })
> ```

> [!NOTE]
>
> 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

> [card] [Setup](https://www.cometchat.com/docs/sdk/android/v4/setup.md)
>
> Configure SDK initialization settings

> [card] [Connection Status](https://www.cometchat.com/docs/sdk/android/v4/connection-status.md)
>
> Monitor SDK connection status changes

> [card] [Login Listeners](https://www.cometchat.com/docs/sdk/android/v4/login-listeners.md)
>
> Handle user authentication events

> [card] [Real-Time Listeners](https://www.cometchat.com/docs/sdk/android/v4/real-time-listeners.md)
>
> Receive real-time messages and events


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.