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

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
}

This hook is used internally by the Chat component. The isOnline and connectionRecovering values are available through the ChatContext.

Parameters

NameTypeRequiredDefaultDescription
clientStreamChatYesThe Stream Chat client instance.
closeConnectionOnBackgroundbooleanNotrueWhether to close the WebSocket connection when the app goes to background.

Returns

PropertyTypeDescription
isOnlineboolean | nullWhether the client has an active connection. null during initial detection.
connectionRecoveringbooleanWhether the connection is currently being recovered after a disruption.