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

# Connection Status

> Guide to monitoring WebSocket connection status using the CometChat iOS SDK CometChatConnectionDelegate.

<Info>
  **Quick Reference for AI Agents & Developers**

  * **Add listener:** `CometChat.addConnectionListener("UNIQUE_ID", self as CometChatConnectionDelegate)`
  * **Remove listener:** `CometChat.removeConnectionListener("UNIQUE_ID")`
  * **Delegate methods:** `connecting()`, `connected()`, `disconnected()`
  * **Related:** [Setup](/sdk/ios/setup) · [WebSocket Connection](/sdk/ios/web-socket-connection-behaviour) · [Overview](/sdk/ios/overview)
</Info>

CometChat SDK provides you with a mechanism to get real-time status of the connection to CometChat web-socket servers. This can be achieved by registering for the events using the `CometChatConnectionDelegate` class.

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.

In order to use the Delegate methods you must add protocol conformance `CometChatConnectionDelegate` as `CometChat.conectiondelegate = self` . Here is the example of CometChatConnectionDelegate:

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

<Accordion title="Sample Payload - CometChatConnectionDelegate Events">
  **connecting() Event:**

  | Parameter | Type | Description                           |
  | --------- | ---- | ------------------------------------- |
  | (none)    | Void | No parameters passed to this callback |

  **Event Trigger:** SDK is attempting to establish WebSocket connection

  | Scenario           | Description                              |
  | ------------------ | ---------------------------------------- |
  | Initial connection | After login, SDK tries to connect        |
  | Reconnection       | After disconnection, SDK auto-reconnects |
  | Network recovery   | When network becomes available again     |

  **connected() Event:**

  | Parameter | Type | Description                           |
  | --------- | ---- | ------------------------------------- |
  | (none)    | Void | No parameters passed to this callback |

  **Event Trigger:** WebSocket connection successfully established

  | Effect           | Description                                           |
  | ---------------- | ----------------------------------------------------- |
  | Real-time events | Start receiving messages, typing indicators, presence |
  | Status           | Connection is active and stable                       |

  **disconnected() Event:**

  | Parameter | Type | Description                           |
  | --------- | ---- | ------------------------------------- |
  | (none)    | Void | No parameters passed to this callback |

  **Event Trigger:** WebSocket connection lost

  | Cause             | Description                              |
  | ----------------- | ---------------------------------------- |
  | Network issues    | WiFi/cellular connection lost            |
  | Server issues     | CometChat server temporarily unavailable |
  | Manual disconnect | `CometChat.disconnect()` called          |

  **Connection Flow:**

  | Step | Event          | Description             |
  | ---- | -------------- | ----------------------- |
  | 1    | disconnected() | Connection breaks       |
  | 2    | connecting()   | SDK auto-reconnects     |
  | 3    | connected()    | Reconnection successful |
</Accordion>

<Note>
  We recommend you to add the Connection Status delegate in your AppDelegate and in your app's first view controller that opens when you log in.
</Note>

You can also get the current connection status by using `getConnectionStatus` property provided by CometChat SDK

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

<Accordion title="Sample Payload - getConnectionStatus">
  **Method:**

  | Method                               | Return Type | Description                         |
  | ------------------------------------ | ----------- | ----------------------------------- |
  | CometChat.getConnectionStatus?.value | String?     | Current WebSocket connection status |

  **Response:**

  | Parameter        | Type    | Description                               |
  | ---------------- | ------- | ----------------------------------------- |
  | connectionStatus | String? | Current status. Example: `"disconnected"` |

  **Possible Values:**

  | Value            | Description                                     |
  | ---------------- | ----------------------------------------------- |
  | `"connecting"`   | SDK is trying to establish WebSocket connection |
  | `"connected"`    | Successfully connected to WebSocket server      |
  | `"disconnected"` | Not connected to WebSocket server               |
</Accordion>

The `CometChat.getConnectionStatus` method will return either of the below 3 values:

1. connecting
2. connected
3. disconnected
