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

> Monitor real-time WebSocket connection status and respond to connectivity changes using the CometChat iOS SDK.

<Accordion title="AI Integration Quick Reference">
  ```swift theme={null}
  // Get current status: "connecting" | "connected" | "disconnected"
  let status = CometChat.getConnectionStatus?.value

  // Listen for connection changes
  // Conform to CometChatConnectionDelegate
  CometChat.addConnectionListener("LISTENER_ID", self as CometChatConnectionDelegate)

  func connecting() { print("Connecting...") }
  func connected() { print("Connected") }
  func disconnected() { print("Disconnected") }

  // Cleanup
  CometChat.removeConnectionListener("LISTENER_ID")
  ```
</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                                                 |
| ---------------- | ---------------- | ----------------------------------------------------------- |
| `"connected"`    | `connected()`    | SDK has an active connection to CometChat servers           |
| `"connecting"`   | `connecting()`   | SDK is attempting to establish or re-establish a connection |
| `"disconnected"` | `disconnected()` | SDK is disconnected due to network issues or other errors   |

## Get Current Status

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

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    var connectionStatus = CometChat.getConnectionStatus?.value
    ```
  </Tab>
</Tabs>

## Listen for Connection Changes

Register a `CometChatConnectionDelegate` to receive real-time connection state updates. We recommend adding this in your `AppDelegate` and in your app's first view controller after login.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    extension AppDelegate: CometChatConnectionDelegate {

        func connecting() {
            print("connecting")
        }
        
        func connected() {
            print("connected")  
        }
        
        func disconnected() {
            print("disconnected")
        }
    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    @interface ViewController ()<CometChatConnectionDelegate>
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        [CometChat connectiondelegate:self];
    }

    - (void)connecting {
        NSLog(@"Connecting");
    }

    - (void)connected {
        NSLog(@"Connected");
    }

    - (void)disconnected {
        NSLog(@"Disconnected");
    }
    ```
  </Tab>
</Tabs>

<Warning>
  Always remove connection listeners when they're no longer needed. 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/ios/web-socket-connection-behaviour">
    Manage WebSocket connection behavior
  </Card>

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

  <Card title="Authentication" icon="right-to-bracket" href="/sdk/ios/authentication-overview">
    Authenticate users with the CometChat SDK
  </Card>

  <Card title="Overview" icon="book" href="/sdk/ios/overview">
    Overview of the CometChat iOS SDK
  </Card>
</CardGroup>
