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
- The querySubscribedUserStatusList interface can be used to actively query the current online status of subscribed users.
Implementation Method
Subscribe Users and Listen to the Online Status Change Event of Subscribed Users
Use the subscribeUsersStatus interface to subscribe to users of interest.
final config = ZIMUserStatusSubscribeConfig();
config.subscriptionDuration = 60 * 24;
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 onUserStatusUpdated.
// Define user online status data
final Map<String, ZIMUserStatus> myUserStatusMap = {};
// User status update callback
ZIMEventHandler.onUserStatusUpdated = (List<ZIMUserStatus> userStatusList) {
for (var status in userStatusList) {
myUserStatusMap[status.userID] = status;
}
// TODO: Refresh UI
};Query the Online Status of Subscribed Users
Use the querySubscribedUserStatusList interface to actively query the online status of subscribed users.
// Query subscription list
final queryConfig = ZIMSubscribedUserStatusQueryConfig(userIDs: ["userIdA", "userIdB"]);
zim.querySubscribedUserStatusList(queryConfig).then((result) {
for (var sub in result.userStatusSubscriptionList) {
myUserStatusMap[sub.userStatus.userID] = sub.userStatus;
}
// TODO: Refresh UI
});
