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
}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
Chatcomponent manages it internally. - Access
isOnlineandconnectionRecoveringthroughuseChatContextinstead. - Set
closeConnectionOnBackgroundtofalseonly if you do not need push notifications while the app is in the background. - Handle the
nullstate ofisOnlineduring initial network detection. - Avoid triggering connection state changes manually; let the hook manage the lifecycle.
Usage
useIsOnline.ts
This hook is used internally by the Chat component. The isOnline and connectionRecovering values are available through the ChatContext.
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. |