> ## Documentation Index
> Fetch the complete documentation index at: https://docs.quickblox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connection

> Learn how to connect to the chat server and set connection settings.

## Before you begin

1. Register a [QuickBlox account](https://admin.quickblox.com/signin). This is a matter of a few minutes and you will be able to use this account to build your apps.
2. Configure QuickBlox SDK for your app. Check out [Setup](/sdks/flutter-setup) page for more details.
3. Create a user session to be able to use QuickBlox functionality. See [Authentication](/sdks/flutter-authentication) page to learn how to do it.

Visit [Key Concepts](/docs/key-concepts) page to learn the most important QuickBlox concepts.

## Connect to Chat server

To connect to Chat server use the code snippet below.

```Dart Dart theme={null}
String login = "chrispeterson";
String password = "superPassword";

try {
  await QB.chat.connect(login, password);
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}
```

## Subscribe to connection state

Subscribe to the connection state changes using the following code snippet.

```Dart Dart theme={null}
//Chat Connections
//QBChatEvents.CONNECTED
//QBChatEvents.CONNECTION_CLOSED
//QBChatEvents.RECONNECTION_FAILED
//QBChatEvents.RECONNECTION_SUCCESSFUL

String event = QBChatEvents.CONNECTED;

try {
  connectedSubscription = await QB.chat.subscribeChatEvent(event, (data) {

  }
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}
```

## Check if connected to Chat server

Check the connection state using the following code snippet.

```Dart Dart theme={null}
try {
  bool? connected = await QB.chat.isConnected();
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}
```

## Disconnect from Chat server

Disconnect from the Chat server using the snippet below.

```Dart Dart theme={null}
try {
  await QB.chat.disconnect();
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}
```

## Enable auto-reconnect to Chat

The SDK reconnects automatically when the connection to the Chat server is lost. There is a way to disable it and then manage it manually.

```Dart Dart theme={null}
try {
  await QB.settings.enableAutoReconnect(true);
} on PlatformException catch (e) {
  // Some error occurred, look at the exception message for more details
}
```

## Manage Chat connections

To provide a seamless chat experience, our SDK manages connections to the Chat server at an application-wide level. Thus, to handle [offline messages](/sdks/flutter-chat-offline-messaging) correctly, use the `disconnect()` method when an app goes to the background and the `connect()` method when an app goes to the foreground. As a result, the SDK will disconnect/connect the current user from the Chat server when the app goes to the background/foreground mode.

```Dart Dart theme={null}
class _SomeScreenState extends State<SomeScreen> with WidgetsBindingObserver {

  //...

  @override
  initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        try {
      	  await QB.chat.connect(userId, userPassword);
        } on PlatformException catch (e) {
      	// Some error occurred, look at the exception message for more details
        }
        break;
      case AppLifecycleState.paused:
        print("app in paused");
        try {
      	  await QB.chat.disconnect();
        } on PlatformException catch (e) {
      	  // Some error occurred, look at the exception message for more details
        }
        break;
    }
  }
}

//...
```

| Argument     | Required | Description               |
| ------------ | -------- | ------------------------- |
| userId       | yes      | The ID of a user.         |
| userPassword | yes      | The password of the user. |
