Subscribe to online status
Subscribe to the online status of selected users.
Online status indicates only whether a user is connected to OpenIMServer. Subscribe only to users visible in the current interface. Each account can subscribe to at most 3,000 users.
const { data: states } = await openimsdk.subscribeUsersStatus(
[...new Set(visibleUserIDs.filter(Boolean))],
);After the Promise succeeds, data contains the current UserOnlineState[] for those users. Subsequent changes arrive through OnUserStatusChanged. Do not subscribe to the entire user directory at once.
Online status fields
| Field | Type | Description |
|---|---|---|
userID | string | The user whose status this is. Use it to find and update that user's status locally. |
status | OnlineState | Aggregate online status. OnlineState.Online means online and OnlineState.Offline means offline. |
platformIDs | Platform[] | Platforms on which the user is currently online. Do not infer a particular device when the field is missing or empty. |
Online status reflects only the connection to OpenIMServer. It does not mean that the user is currently viewing the application, a conversation, or a message.
Listen for online status changes
This page provides the complete OnUserStatusChanged listener. Display the initial status returned by the subscription, then use userID to update the matching user when an event arrives.
import { SdkEvent } from '@openim/wasm-client-sdk';
const handleStatusChanged = ({ data }) => {
presenceByUserID.set(data.userID, data);
};
openimsdk.on(SdkEvent.OnUserStatusChanged, handleStatusChanged);
function removeStatusListener() {
openimsdk.off(SdkEvent.OnUserStatusChanged, handleStatusChanged);
}Call removeStatusListener() when signing out, switching accounts, or destroying the online-status state layer.
Was this page helpful?