logo
On this page

Online Status Subscription


Note

To use this feature, please subscribe to the enterprise plan.

Introduction

User online statuses ZIMUserOnlineStatus include three types: online, logout, and offline.

  • Online: When the user actively calls the login method to log in and maintains a network connection, the user's status changes to online.
  • Logout: After the user actively calls the logout method, the user's status changes to logout.
  • Offline: After the user logs in by calling the login method, if the user kills the app or locks the screen, or the app is in the background, causing the network to disconnect, the user's status changes to offline.

This article introduces how to subscribe to and query users' online statuses.

Subscribe to user online statuses

To continuously monitor the online status of certain users, you can call the subscribeUsersStatus method and pass the target user IDs in the userIDs parameter (up to 100 registered users, excluding the subscriber itself). You also need to pass the subscriptionDuration in the config (during which the online status changes of the target users will be continuously monitored), so that the target users can be added in the subscriber's online status subscription list.

Note
  • A user can subscribe to up to 3,000 people. When the number of subscribed users exceeds 3,000, the earliest subscribed users will be replaced.
  • After a user subscribing to another user's online status, the subscription remains valid even if the user logs out and logs back in within the subscriptionDuration period. There's no need to resubscribe.
Untitled
ZIMUserStatusSubscribeConfig config;
config.subscriptionDuration = 60; // Subscription duration in minutes, valid range is 1 to 43200 (30 days)

std::vector<std::string> userIDs = {"userIdA", "userIdB"};

zim_->subscribeUsersStatus(userIDs, config, [](const std::vector<ZIMErrorUserInfo> &errorUserList, const ZIMError &errorInfo) {
    for (const ZIMErrorUserInfo &errorUserInfo : errorUserList) {
        errorUserInfo.userID;   // User ID failed to subscribe
        errorUserInfo.reason;   // Failure reason
    }
});
1
Copied!

Event notification

Subsequently, the online status changes of the target users within the specified subscriptionDuration will be returned through the onUserStatusUpdated event callback registered by the ZIMEventHandler method.

Note

If the user logs off and then logs back in after subscribing, the ZIM SDK will proactively notify the user via the callback about the last status changes of the subscribed target users during the user’s offline period.

Untitled
class CZIMEventHandler :public zim::ZIMEventHandler
{
    public:
        CZIMEventHandler();
        ~CZIMEventHandler();
    private:
        virtual void onUserStatusUpdated(ZIM* zim, const std::vector<ZIMUserStatus>& userStatusList) override;
        // Other callbacks
}

...

im_event_handler_ = std::make_shared<CZIMEventHandler>();
zim_->setEventHandler(im_event_handler_);
1
Copied!
Untitled
void onUserStatusUpdated(ZIM* zim, const std::vector<ZIMUserStatus>& userStatusList)  {
    for (const ZIMUserStatus& userStatus : userStatusList) {
        userStatus.userID;            // Target user ID
        userStatus.onlineStatus;      // Target user's online status
        userStatus.onlinePlatforms;   // User online platforms list 
        userStatus.lastUpdateTime;    // Last update time of online status
    }
}
1
Copied!

Unsubscribe from user online statuses

If you no longer need to monitor the online status of certain users, you can call the unsubscribeUsersStatus method and pass the target user IDs in the userIDs parameter (up to 100 users).

Untitled
std::vector<std::string> userIDs = {"userIdA", "userIdB"};

zim_->unsubscribeUsersStatus(userIDs, [](const std::vector<ZIMErrorUserInfo> &errorUserList, const ZIMError &errorInfo) {
    for (const ZIMErrorUserInfo &errorUserInfo : errorUserList) {
        errorUserInfo.userID;   // User ID failed to unsubscribe
        errorUserInfo.reason;   // Failure reason
    }
});
1
Copied!

Query user online statuses

If you only need to get the online statuses of target users once, you can call the queryUsersStatus method and pass the target user IDs (up to 200 users, excluding the current user).

Untitled
std::vector<std::string> userIDs = {"userIdA", "userIdB"};

zim_->queryUsersStatus(userIDs, [](const std::vector<ZIMUserStatus> &userStatusList,
                                    const std::vector<ZIMErrorUserInfo> &errorUserList,
                                    const ZIMError &errorInfo) {
    for (const ZIMUserStatus &userStatus : userStatusList) {
        userStatus.userID;          // Target user ID
        userStatus.onlineStatus;    // User online status
        userStatus.onlinePlatforms; // User online platforms list
        userStatus.lastUpdateTime;  // Last update time of online status
    }
    for (const ZIMErrorUserInfo &errorUserInfo : errorUserList) {
        errorUserInfo.userID; // Failed user ID
        errorUserInfo.reason; // Failure reason
    }
});
1
Copied!

Query the online status subscription list

If users want to know their own online status subscription list, they can use the querySubscribedUserStatusList method. If the config.userIDs parameter is empty, the complete subscription list will be returned, including the current status and subscription duration of the target users.

If the config.userIDs parameter is not empty, it means the user wants to check if certain users are in the subscription list. The result callback will only include the status information of users who are in the list.

Untitled
ZIMSubscribedUserStatusQueryConfig config;
// Target user IDs for query (up to 200 users in a single query)
// When userIDs is empty, it means getting the complete subscription table information
// When userIDs is not empty, it means checking if the target users are in the subscription list
// If they are, the result will include their status information
// If they are not, the result will not include any related information
config.userIDs = {"userIdA", "userIdB"};

zim_->querySubscribedUserStatusList(config, [](const std::vector<ZIMUserStatusSubscription> &userStatusSubscriptionList,
                                                const ZIMError &errorInfo) {
});
1
Copied!

Listen to the online status of the current user on the current device

If you want to know the online status of the current user on the current device, you can listen to the onConnectionStateChanged callback to get the current connection status of the user ZIMConnectionState, and determine the online status of the current user based on it.

In ZIMConnectionState, ZIM_CONNECTION_STATE_CONNECTED means the user is online, ZIM_CONNECTION_STATE_DISCONNECTED means the user is offline. The remaining ZIM_CONNECTION_STATE_CONNECTING and ZIM_CONNECTION_STATE_RECONNECTING connection states can be determined as either online or offline based on your business logic.

Untitled
@interface ZIMEventHandlerImpl : NSObject<ZIMEventHandler>

+(ZIMEventHandlerImpl *)getInstance();

@end

@implementation ZIMEventHandlerImpl

- (void)connectionStateChanged:(ZIMConnectionState)state event:(ZIMConnectionEvent)event extendedData:(NSDictionary *)extendedData{
    switch (state) {
        case ZIMConnectionStateConnected:
            // Connected, you can map the current user's status as online
            break;
        case ZIMConnectionStateConnecting:
            // Connecting, you can map the current user's status as online or offline based on your business logic
            break;
        case ZIMConnectionStateReconnecting:
            // Reconnecting, you can map the current user's status as online or offline based on your business logic
            break;
        case ZIMConnectionStateDisconnected:
            // Disconnected, you can map the current user's status as offline
            break;
        default:
            break;
    }
}

// Other callback events..

@end

ZIMEventHandlerImpl *eventHandlerImpl = [ZIMEventHandlerImpl getInstance];

[zim setEventHandler: eventHandlerImpl];

1
Copied!
Untitled
class CZIMEventHandler :public zim::ZIMEventHandler
{
    public:
        CZIMEventHandler();
        ~CZIMEventHandler();
    private:
        virtual void onConnectionStateChanged(ZIM* /*zim*/, ZIMConnectionState state, ZIMConnectionEvent event, const std::string & /*extendedData*/) override;
        // Other callbacks
}

...

im_event_handler_ = std::make_shared<CZIMEventHandler>();
zim_->setEventHandler(im_event_handler_);
1
Copied!
Untitled
void onConnectionStateChanged(ZIM* /*zim*/, ZIMConnectionState state, ZIMConnectionEvent event, const std::string & /*extendedData*/) {
    switch (state) {
        case ZIM_CONNECTION_STATE_CONNECTED:
            // Connected, you can map the current user's status as online
            break;
        case ZIM_CONNECTION_STATE_CONNECTING:
            // Connecting, you can map the current user's status as online or offline based on your business logic
            break;
        case ZIM_CONNECTION_STATE_RECONNECTING:
            // Reconnecting, you can map the current user's status as online or offline based on your business logic
            break;
        case ZIM_CONNECTION_STATE_DISCONNECTED:
            // Disconnected, you can map the current user's status as offline
            break;
        default:
            // Other possible states
            break;
    }
}
1
Copied!

Listen to the online platforms list of the current user

In the multi-login scenario, when a user logs in to a second platform, you can listen to the onUserStatusUpdated callback to get the current user's online platforms from the userStatus.onlinePlatforms property. You can use this information to display the user's online status on different platforms (e.g., showing that the user is online on iOS and Windows).

Note

When the onUserStatusUpdated callback returns the userStatus information of the current user, the onlineStatus will be unknown and lastUpdateTime will be 0, which cannot accurately reflect the online status.

Untitled
void onUserStatusUpdated(ZIM* /*zim*/, const std::vector<ZIMUserStatus>& userStatusList) {
    for (const ZIMUserStatus& userStatus : userStatusList) {
        if (userStatus.userID == "myUserID") {  // Check if it is the current user
            userStatus.onlineStatus;            // If it is the current user, onlineStatus will be UNKNOWN
            userStatus.onlinePlatforms;         // List of online platforms for the current user
            userStatus.lastUpdateTime;          // If it is the current user, lastUpdateTime will be 0
        }
    }
}
1
Copied!

Previous

Friend management

Next

Manage rooms