SDKsReact Native
Subscribe to online status
Subscribe to user online status with the OpenIM React Native SDK.
Call subscribeUsersStatus() to subscribe to the online status of specific users. Subscribe only to users whose status the current screen needs to display.
const userIDs = [...new Set(visibleUserIDs.filter(Boolean))];
const states = await OpenIMSDK.subscribeUsersStatus(userIDs);The method returns the current UserOnlineState[] for these users:
| Field | Type | Description |
|---|---|---|
userID | string | ID of the user whose status is reported. |
status | OnlineState | OnlineState.Online means online; OnlineState.Offline means offline. |
platformIDs | Platform[] (optional) | Platforms on which the user is currently online. |
Online status indicates whether the user is connected to OpenIMServer. It does not indicate whether the user is viewing the app or a conversation.
Listen for online-status changes
When a subscribed user's status changes, OnUserStatusChanged is emitted:
const handleStatusChanged = (state: UserOnlineState) => {
// Update the status for state.userID
};
// Add the listener
OpenIMSDK.on(OpenIMEvent.OnUserStatusChanged, handleStatusChanged);
// Remove the listener
OpenIMSDK.off(OpenIMEvent.OnUserStatusChanged, handleStatusChanged);Cancel subscriptions and remove the listener when logging out, switching accounts, or when those users no longer need to be displayed.
Was this page helpful?