logo
In-app Chat
SDK Error Codes
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 onUserStatusUpdated 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 = new ZIMUserStatusSubscribeConfig();
config.setSubscriptionDuration(60 * 24); // 1 day
zim.subscribeUsersStatus(Arrays.asList("userIdA", "userIdB"), config, (errorUserList, errorInfo) -> {
    
});

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

// Define user online status data
Map<String, ZIMUserStatus> myUserStatusMap = new HashMap<>();

// User status update callback
public void onUserStatusUpdated(ArrayList<ZIMUserStatus> userStatusList) {
    for (ZIMUserStatus status : userStatusList) {
        myUserStatusMap.put(status.getUserID(), 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
ZIMSubscribedUserStatusQueryConfig queryConfig = new ZIMSubscribedUserStatusQueryConfig();
queryConfig.setUserIDs(Arrays.asList("userIdA", "userIdB"));
zim.querySubscribedUserStatusList(queryConfig, (userStatusSubscriptionList, errorInfo) -> {
    for (ZIMUserStatusSubscription subscription : userStatusSubscriptionList) {
        myUserStatusMap.put(subscription.getUserStatus().getUserID(), subscription.getUserStatus());
    }
    // TODO: Refresh UI
});

Previous

Render Conversation Messages on the Chat Page

Next

Overview

On this page

Back to top