> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-angular-v5-docs-update.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Connection Status

> Monitor real-time WebSocket connection status with CometChat SDK using ConnectionListener callbacks and getConnectionStatus method.

<Accordion title="AI Integration Quick Reference">
  ```dart theme={null}
  // Add connection listener
  CometChat.addConnectionListener("connection_listener", ConnectionListenerImpl());

  // Connection listener implementation
  class ConnectionListenerImpl with ConnectionListener {
    @override
    void onConnected() => debugPrint("Connected");
    
    @override
    void onConnecting() => debugPrint("Connecting...");
    
    @override
    void onDisconnected() => debugPrint("Disconnected");
    
    @override
    void onFeatureThrottled() => debugPrint("Feature throttled");
    
    @override
    void onConnectionError(CometChatException error) => debugPrint("Error: ${error.message}");
  }

  // Check current connection status
  String status = CometChat.getConnectionStatus();
  // Returns: CometChatWSState.connected, connecting, disconnected, or featureThrottled

  // Remove listener when done
  CometChat.removeConnectionListener("connection_listener");
  ```
</Accordion>

The CometChat SDK maintains a WebSocket connection to CometChat servers for real-time events. You can check the current connection state and listen for changes — useful for showing connectivity indicators in your UI or queuing operations while offline.

When the connection drops, the SDK automatically attempts to reconnect, cycling through `disconnected` → `connecting` → `connected`.

## Connection States

| Value                               | Callback                                | Description                                                 |
| ----------------------------------- | --------------------------------------- | ----------------------------------------------------------- |
| `CometChatWSState.connected`        | `onConnected()`                         | SDK has an active connection to CometChat servers           |
| `CometChatWSState.connecting`       | `onConnecting()`                        | SDK is attempting to establish or re-establish a connection |
| `CometChatWSState.disconnected`     | `onDisconnected()`                      | SDK is disconnected due to network issues or other errors   |
| `CometChatWSState.featureThrottled` | `onFeatureThrottled()`                  | A feature has been throttled to prevent performance loss    |
| —                                   | `onConnectionError(CometChatException)` | An error occurred while maintaining the connection          |

## Get Current Status

Use `getConnectionStatus()` to check the current connection state at any time:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    String connectionStatus = CometChat.getConnectionStatus();
    ```
  </Tab>
</Tabs>

The method returns one of the following values:

1. `CometChatWSState.connected` (connected)
2. `CometChatWSState.connecting` (connecting)
3. `CometChatWSState.disconnected` (disconnected)
4. `CometChatWSState.featureThrottled` (featureThrottled)

## Listen for Connection Changes

Register a `ConnectionListener` to receive real-time connection state updates. We recommend adding this on app startup after `CometChat.init()` completes.

### ConnectionListener Events

| Event                                         | Parameter            | Description                                                                                     |
| --------------------------------------------- | -------------------- | ----------------------------------------------------------------------------------------------- |
| `onConnected()`                               | —                    | Triggered when the SDK successfully establishes a connection to the WebSocket server            |
| `onConnecting()`                              | —                    | Triggered when the SDK is attempting to establish a connection to the WebSocket server          |
| `onDisconnected()`                            | —                    | Triggered when the SDK gets disconnected due to network fluctuations or other issues            |
| `onFeatureThrottled()`                        | —                    | Triggered when CometChat automatically toggles off certain features to prevent performance loss |
| `onConnectionError(CometChatException error)` | `CometChatException` | Triggered when an error occurs while maintaining the WebSocket connection                       |

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    class Class_Name  with ConnectionListener {
    //1. Register Connection listener
    //CometChat.addConnctionListener("listenerId", this);

    //2. Ovveride the ConnectionListener methods
    @override
    void onConnected() {
      // TODO: implement onConnected
    }

    @override
    void onConnecting() {
      // TODO: implement onConnecting
    }

    @override
    void onDisconnected() {
      // TODO: implement onDisconnected
    }

    @override
    void onFeatureThrottled() {
      // TODO: implement onFeatureThrottled
    }

    @override
    void onConnectionError(CometChatException error) {
      // TODO: implement onFeatureThrottled
    }

    }
    ```
  </Tab>
</Tabs>

<Warning>
  Always remove connection listeners when they're no longer needed (e.g., on widget dispose or navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.
</Warning>

### Remove Connection Listener

```dart theme={null}
CometChat.removeConnectionListener("listenerId");
```

<Note>
  Know more about CometChat SDK connection behaviour [click here](/sdk/flutter/connection-behaviour)
</Note>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Connection Behaviour" icon="wifi" href="/sdk/flutter/connection-behaviour">
    Understand how CometChat SDK manages WebSocket connections
  </Card>

  <Card title="Login Listeners" icon="user-check" href="/sdk/flutter/authentication-overview#login-listener">
    Monitor user login and logout events in real-time
  </Card>

  <Card title="All Real-Time Listeners" icon="tower-broadcast" href="/sdk/flutter/all-real-time-listeners">
    Complete reference for all SDK listeners
  </Card>

  <Card title="Setup SDK" icon="gear" href="/sdk/flutter/setup">
    Install and initialize the CometChat SDK
  </Card>
</CardGroup>
