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.

ZIMUserStatusSubscribeConfig *config = [[ZIMUserStatusSubscribeConfig alloc] init];

config.subscriptionDuration = 60 *24; // Subscription duration (in minutes), valid range is 1 to 43200 (30 days)

[[ZIM getInstance] subscribeUsersStatus:@[@"userIdA",@"userIdB"] // Please enter the users that need to be subscribed

                   config:config

                 callback:^(NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,

                            ZIMError *_Nonnull errorInfo) {}];
                            

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
NSMutableDictionary<NSString *,ZIMUserStatus *> myUserStatusMap;

// User status update callback
- (void)zim:(ZIM *)zim userStatusUpdated:(NSArray<ZIMUserStatus *> *)userStatusList {
    for (ZIMUserStatus *userStatus in userStatusList) {
        // Use userID as the key, replace if it exists, add if it doesn't exist
        myUserStatusList[userStatus.userID] = userStatus;
    }
    // At this point, the UI can be refreshed or other operations can be performed
}

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 querySubscribedUserStatusListWithConfig interface to actively query the online status of subscribed users.

ZIMSubscribedUserStatusQueryConfig *config = [[ZIMSubscribedUserStatusQueryConfig alloc] init];

// Query target user IDs (a single query can include up to 200 users)
// When userIDs is empty, it indicates the need to retrieve full subscription table information
// When userIDs is not empty, it indicates the need to check if the target users are in the subscription list
// If they exist, the result callback will include the user status information
// If they do not exist, the result callback will not include relevant information
config.userIDs = @[@"userIdA",@"userIdB"]; // Please enter the users that need to be subscribed

Previous

ZIM

Next

Render Conversation Messages on the Chat Page

On this page

Back to top