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

# Managing Web Socket Connections Manually

> Guide to manually controlling WebSocket connections in the CometChat iOS SDK for advanced use cases.

<Accordion title="AI Integration Quick Reference">
  * **Connect:** `CometChat.connect()`
  * **Disconnect:** `CometChat.disconnect()`
  * **Disable auto-connect:** `AppSettings.AppSettingsBuilder().autoEstablishSocketConnection(false).build()`
  * **Use case:** Manual control for battery optimization or specific app flows
  * **Related:** [Connection Status](/sdk/ios/connection-status) · [Connection Behaviour](/sdk/ios/web-socket-connection-behaviour) · [Setup](/sdk/ios/setup)
</Accordion>

## Default SDK behaviour on login

When the login method of the SDK is called, the SDK performs the below operations:

1. Logs the user into the SDK
2. Saves the details of the logged in user locally.
3. Creates a web-socket connection for the logged in user.

This makes sure that the logged in user starts receiving real-time messages as soon as he logs in.

When the app is reopened and the init() method is called, the web-socket connection to the server is established automatically.

If you wish to connect and disconnect to the web-socket server manually, refer to the section below.

## Managing the Web-socket connections manually

The CometChat SDK allows you to take control of the web-socket connection. Follow these steps:

1. While calling the init() function on the app startup, use the `autoEstablishSocketConnection()` method provided by the `AppSettingsBuilder` class. If set to `true`, the SDK manages the web-socket connection internally. If set to `false`, you handle it manually:

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let appSettings = AppSettings.AppSettingsBuilder()
    									.setRegion(region: "us")
    									.autoEstablishSocketConnection(false)
    									.build()

    let _ =  CometChat.init(appId:"1976246d33493296",
                            appSettings: appSettings,
                            onSuccess: { (Success) in
                print( "Initialization onSuccess \(Success)")
            }) { (error) in
                print( "Initialization Error Code:  \(error.errorCode)")
                print( "Initialization Error Description:  \(error.errorDescription)")
            }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    AppSettingsBuilder *appSettingBuilder = [[AppSettingsBuilder alloc]init];
    AppSettings *appSettings = [[[appSettingBuilder autoEstablishSocketConnection:false]
    														setRegionWithRegion: REGION_CODE]build];
    [[CometChat alloc]initWithAppId:APP_ID appSettings:appSettings onSuccess:^(BOOL isSuccess) {
    	NSLog(isSuccess ? @"CometChat Initialize Success:-YES" : @"CometChat Initialize Success:-NO");
    } onError:^(CometChatException * error) {
    	NSLog(@"Error %@",[error errorDescription]);
    }];
    ```
  </Tab>
</Tabs>

2. You can manage the connection to the web-socket server using the `connect()` and `disconnect()` methods provided by the SDK.
3. **Connect to the web-socket server**

Use the `connect()` method provided by the `CometChat` class to establish the connection. Make sure the user is logged in before calling this method (`CometChat.getLoggedInUser()`). Once connected, you will start receiving all real-time events for the logged in user.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    CometChat.connect()
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    CometChat.connect();
    ```
  </Tab>
</Tabs>

2. **Disconnect from the web-socket server**

Use the `disconnect()` method to break the established connection. Once disconnected, you will stop receiving all real-time events.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    CometChat.disconnect()
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    CometChat.disconnect();
    ```
  </Tab>
</Tabs>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Connection Status" icon="signal" href="/sdk/ios/connection-status">
    Monitor the SDK connection state in real time
  </Card>

  <Card title="Connection Behaviour" icon="arrows-rotate" href="/sdk/ios/web-socket-connection-behaviour">
    Understand default WebSocket connection lifecycle
  </Card>

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

  <Card title="Setup SDK" icon="gear" href="/sdk/ios/setup">
    SDK installation and initialization guide
  </Card>
</CardGroup>
