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

# Connection Status

> Monitor CometChat WebSocket connection status in JavaScript apps with connecting, connected, and disconnected callbacks.

CometChat SDK provides you with a mechanism to get real-time status of the connection to CometChat web-socket servers.

Connection Status provides you with the below 3 methods to get the status of the connection to CometChat web-socket servers:

| Delegate Method | Information                                                                                                                                      |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| connecting      | This method is triggered when CometChat SDK is trying to establish a connection to the web-socket server.                                        |
| connected       | This method is called when CometChat SDK has successfully established a connection and now is connected.                                         |
| disconnected    | This method is called when the CometChat SDK gets disconnected due to any issue while maintaining the connection like network fluctuations, etc. |

Once the connection is broken, the disconnected callback is triggered, the SDK automatically tries to establish the connection again, thus going into the connecting state and triggering the `connecting` method. Once the attempt to connect is successful, the `connected` method is triggered thus letting the developer know that the connection is established and is active.

To receive real-time connection status, you need to register `ConnectionListener` wherever you wish to receive the real-time status. You can use the `addConnectionListener()` method to do so.

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    const connectionStatus: string = CometChat.getConnectionStatus();
    ```
  </Tab>

  <Tab title="JavaScript">
    ```javascript theme={null}
    const connectionStatus = CometChat.getConnectionStatus();
    ```
  </Tab>
</Tabs>

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

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    let listenerID: string = "UNIQUE_LISTENER_ID";
    CometChat.addConnectionListener(
      listenerID,
      new CometChat.ConnectionListener({
        onConnected: () => {
          console.log("ConnectionListener => On Connected");
        },
        inConnecting: () => {
          console.log("ConnectionListener => In connecting");
        },
        onDisconnected: () => {
          console.log("ConnectionListener => On Disconnected");
        },
      })
    );
    ```
  </Tab>

  <Tab title="JavaScript">
    ```javascript theme={null}
    let listenerID = "UNIQUE_LISTENER_ID";
    CometChat.addConnectionListener(
      listenerID,
      new CometChat.ConnectionListener({
        onConnected: () => {
          console.log("ConnectionListener => On Connected");
        },
        inConnecting: () => {
          console.log("ConnectionListener => In connecting");
        },
        onDisconnected: () => {
          console.log("ConnectionListener => On Disconnected");
        },
      })
    );
    ```
  </Tab>
</Tabs>

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

***

## Next Steps

<CardGroup cols={2}>
  <Card title="WebSocket Management" icon="plug" href="/sdk/javascript/managing-web-sockets-connections-manually">
    Manually manage WebSocket connections
  </Card>

  <Card title="Login Listener" icon="right-to-bracket" href="/sdk/javascript/authentication-overview#login-listener">
    Listen for login and logout events
  </Card>

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

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