# useIsOnline

Tracks the network connectivity and WebSocket connection state of the Stream Chat client. It disconnects the WebSocket when the app goes to the background and reconnects when it returns to the foreground, ensuring push notifications are received while the app is backgrounded.

## Best Practices

- Avoid calling this hook directly; the `Chat` component manages it internally.
- Access `isOnline` and `connectionRecovering` through `useChatContext` instead.
- Set `closeConnectionOnBackground` to `false` only if you do not need push notifications while the app is in the background.
- Handle the `null` state of `isOnline` during initial network detection.
- Avoid triggering connection state changes manually; let the hook manage the lifecycle.

## Usage

```tsx title="useIsOnline.ts"
import { useIsOnline } from "stream-chat-react-native";

const { isOnline, connectionRecovering } = useIsOnline(client, true);

if (connectionRecovering) {
  // Show a reconnecting indicator
}

if (!isOnline) {
  // Show an offline banner
}
```

>
> **Note:** This hook is used internally by the `Chat` component. The `isOnline` and `connectionRecovering` values are available through the [`ChatContext`](https://getstream.io/chat/docs/sdk/react-native/contexts/chat-context/).
>

## Parameters

| Name                        | Type         | Required | Default | Description                                                                |
| --------------------------- | ------------ | -------- | ------- | -------------------------------------------------------------------------- |
| client                      | `StreamChat` | Yes      |         | The Stream Chat client instance.                                           |
| closeConnectionOnBackground | `boolean`    | No       | `true`  | Whether to close the WebSocket connection when the app goes to background. |

## Returns

| Property             | Type                | Description                                                                   |
| -------------------- | ------------------- | ----------------------------------------------------------------------------- |
| isOnline             | `boolean` \| `null` | Whether the client has an active connection. `null` during initial detection. |
| connectionRecovering | `boolean`           | Whether the connection is currently being recovered after a disruption.       |

---

For the most recent version of this documentation, visit [https://getstream.io/chat/docs/sdk/react-native/hooks/chat/use-is-online/](https://getstream.io/chat/docs/sdk/react-native/hooks/chat/use-is-online/).