logo
On this page

Subscribe to User Online Status


Overview

In some scenarios (such as social chat, meetings, etc.), you may need to subscribe to the online status of users to determine whether the user is online, offline, or logged out.

This article introduces how to use the ZIM SDK to subscribe to user online status.

Prerequisites

The ZIM SDK has already been integrated into the project and basic message sending and receiving functionality has been implemented. For details, please refer to Send and Receive Messages.

Get User Online Status

There are two ways to get user online status: Real-time Get User Online Status

  • userA subscribed to userB's online status through the subscribeUsersStatus interface and listened to userB's online status update event.
  • After userB's online status is updated (for example, the user is offline from online status), userA will receive a callback notification from userStatusUpdated to get userB's latest online status.

Active Query User Online Status

Implementation Method

Subscribe Users and Listen to the Online Status Change Event of Subscribed Users

Note
This method is suitable for real-time getting the online status of subscribed users.

Use the subscribeUsersStatus interface to subscribe to users of interest.

const config = { subscriptionDuration: 60 * 24 }; // 1 day
zim.subscribeUsersStatus(['userIdA','userIdB'], config)
   .then((errorUserList) => {
       
});

Listen to the user online status change event, and the online status change of subscribed users will be returned through userStatusUpdated.

// Define user online status data
const myUserStatusMap: Record<string, ZIMUserStatus> = {};

// User status update callback
zim.on('userStatusUpdated', (userStatusList: ZIMUserStatus[]) => {
  userStatusList.forEach(status => {
    myUserStatusMap[status.userID] = status;
  });
  // TODO: Refresh UI
});

Query the Online Status of Subscribed Users

Note
This method is suitable for single-time getting the current online status of subscribed users.

Use the querySubscribedUserStatusList interface to actively query the online status of subscribed users.

// Query subscription list
const queryConfig = { userIDs: ['userIdA','userIdB'] };
zim.querySubscribedUserStatusList(queryConfig)
   .then(result => {
       result.userStatusSubscriptionList.forEach(sub => {
           myUserStatusMap[sub.userStatus.userID] = sub.userStatus;
       });
       // TODO: Refresh UI
   });

Previous

ZIM

Next

Render Conversation Messages on the Chat Page

On this page

Back to top