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

# Connections

When you [instantiate](/docs/chat/setup#instantiate) a client, a realtime connection is established and maintained with Ably. You can interact with the connection using the `ChatClient.connection` object to monitor a client's connection status.

## Connection Statuses

A connection can have any of the following statuses:

| Status         | Description                                                                                               |
| -------------- | --------------------------------------------------------------------------------------------------------- |
| `initialized`  | A connection object has been initialized but not yet connected.                                           |
| `connecting`   | A connection attempt has been initiated.                                                                  |
| `connected`    | A connection exists and is active.                                                                        |
| `disconnected` | A temporary failure condition when no current connection exists.                                          |
| `suspended`    | A long term failure condition when no current connection exists because there is no network connectivity. |
| `closing`      | An explicit request by the developer to close the connection has been sent.                               |
| `closed`       | The connection has been explicitly closed by the client.                                                  |
| `failed`       | This status is entered if the SDK encounters a failure condition that it cannot recover from.             |

Use the `status` property to check which status a connection is currently in:

<Tabs>
  <Tab name="JavaScript">
    <Code>
      ```javascript theme={null}
      const connectionStatus = chatClient.connection.status;
      const error = chatClient.connection.error;
      ```
    </Code>
  </Tab>

  <Tab name="React">
    <Code>
      ```react theme={null}
      import { useChatConnection } from '@ably/chat/react';

      const MyComponent = () => {
        const { currentStatus } = useChatConnection({
          onStatusChange: (statusChange) => {
            console.log('Connection status changed to: ', statusChange.current);
          },
        });
        return <div>Connection status is: {currentStatus}</div>;
      };
      ```
    </Code>
  </Tab>

  <Tab name="Swift">
    <Code>
      ```swift theme={null}
      let status = chatClient.connection.status
      ```
    </Code>
  </Tab>

  <Tab name="Android / Kotlin">
    <Code>
      ```android theme={null}
      val connectionStatus = chatClient.connection.status
      ```
    </Code>
  </Tab>
</Tabs>

## Monitor Connection Status

Register a listener to monitor connection status changes:

<Tabs>
  <Tab name="JavaScript">
    <Code>
      ```javascript theme={null}
      const { off } = chatClient.connection.onStatusChange((change) => console.log(change));
      ```
    </Code>
  </Tab>

  <Tab name="React">
    <Code>
      ```react theme={null}
      import { useOccupancy } from '@ably/chat/react';

      const MyComponent = () => {
        useOccupancy({
          onConnectionStatusChange: (connectionStatusChange) => {
            console.log('Connection status change:', connectionStatusChange);
          },
        });
        return <div>Occupancy Component</div>;
      };
      ```
    </Code>
  </Tab>

  <Tab name="Swift">
    <Code>
      ```swift theme={null}
      let subscription = chatClient.connection.onStatusChange()
      for await statusChange in subscription {
        print("Connection status changed to: \(statusChange.current)")
      }
      ```
    </Code>
  </Tab>

  <Tab name="Android / Kotlin">
    <Code>
      ```android theme={null}
      val (off) = chatClient.connection.onStatusChange { statusChange: ConnectionStatusChange ->
          println(statusChange.toString())
      }
      ```
    </Code>
  </Tab>
</Tabs>

## Handle Discontinuity

If a client briefly loses connection to Ably, for example when driving through a tunnel, the SDK will attempt to recover the connection. If the disruption lasts for less than 2 minutes, then on reconnection the SDK will automatically reattach to any rooms and replay any missed messages.

During periods of discontinuity greater than 2 minutes then you will need to take steps to recover any missed messages, such as by calling [history](/docs/chat/rooms/history).

The Chat SDK provides an `onDiscontinuity()` handler exposed via the Room object to assist with this:

<Tabs>
  <Tab name="JavaScript">
    <Code>
      ```javascript theme={null}
      const { off } = room.onDiscontinuity((reason: ErrorInfo) => {
        // Recover from the discontinuity
      });
      ```
    </Code>
  </Tab>

  <Tab name="React">
    <Code>
      ```react theme={null}
      import { useState } from 'react';
      import { useMessages } from '@ably/chat/react';

      const MyComponent = () => {
        useMessages({
          onDiscontinuity: (error) => {
            console.log('Discontinuity detected:', error);
          },
        });

        return <div>...</div>;
      };
      ```
    </Code>
  </Tab>

  <Tab name="Swift">
    <Code>
      ```swift theme={null}
      let subscription = room.onDiscontinuity()
      for await error in subscription {
        print("Recovering from the error: \(error)")
      }
      ```
    </Code>
  </Tab>

  <Tab name="Android / Kotlin">
    <Code>
      ```android theme={null}
      val (off) = room.onDiscontinuity { reason: ErrorInfo ->
        // Recover from the discontinuity
      }
      ```
    </Code>
  </Tab>
</Tabs>

## Next Steps

* Learn about [rooms](/docs/chat/rooms)
* Explore [message features](/docs/chat/rooms/messages)
* Understand [presence](/docs/chat/rooms/presence)
