Online Status Subscription
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.
- 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.
ZIMUserStatusSubscribeConfig config = new ZIMUserStatusSubscribeConfig();
config.subscriptionDuration = 60; // Subscription duration in minutes, valid range is 1 to 43200 (30 days)
ArrayList<String> userIDs = new ArrayList<>();
userIDs.add("userIdA");
userIDs.add("userIdB");
zim.subscribeUsersStatus(userIDs, config, new ZIMUsersStatusSubscribedCallback() {
@Override
public void onUsersStatusSubscribed(ArrayList<ZIMErrorUserInfo> errorUserList, ZIMError errorInfo) {
// Handle subscription callback
for (ZIMErrorUserInfo errorUserInfo : errorUserList) {
System.out.println("User ID failed to subscribe: " + errorUserInfo.userID);
System.out.println("Failure reason: " + errorUserInfo.reason);
}
}
});
1
ZIMUserStatusSubscribeConfig *config = [[ZIMUserStatusSubscribeConfig alloc] init];
config.subscriptionDuration = 60; // Subscription duration in minutes, valid range is 1 to 43200 (30 days)
[zim subscribeUsersStatus:@[@"userIdA",@"userIdB"]
config:config
callback:^(NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,
ZIMError *_Nonnull errorInfo) {
for(ZIMErrorUserInfo *errorUserInfo in errorUserList){
errorUserInfo.userID; // User ID of the failed subscription
errorUserInfo.reason; // Error code of the failed subscription
}
}];
1
ZIMUserStatusSubscribeConfig *config = [[ZIMUserStatusSubscribeConfig alloc] init];
config.subscriptionDuration = 60; // Subscription duration in minutes, valid range is 1 to 43200 (30 days)
[zim subscribeUsersStatus:@[@"userIdA",@"userIdB"]
config:config
callback:^(NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,
ZIMError *_Nonnull errorInfo) {
for(ZIMErrorUserInfo *errorUserInfo in errorUserList){
errorUserInfo.userID; // User ID of the failed subscription
errorUserInfo.reason; // Error code of the failed subscription
}
}];
1
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
// Create the subscription configuration
const config = {
subscriptionDuration: 60, // Subscription duration in minutes, valid range is 1 to 43200 (30 days)
};
// Subscription user ID list
const userIDs = ['userIdA', 'userIdB'];
try {
// Call the subscribeUsersStatus method
const result = await zim.subscribeUsersStatus(userIDs, config);
// Check for users with failed subscriptions
if (result.errorUserList.length > 0) {
result.errorUserList.forEach((errorUserInfo) => {
console.log('User ID with failed subscription: ', errorUserInfo.userID);
console.log('Error code for failed subscription: ', errorUserInfo.reason);
});
} else {
console.log('All users subscribed successfully!');
}
} catch (error) {
// Catch the exception and handle the error code
console.error('Error occurred while subscribing:', error);
}
1
ZIMUserStatusSubscribeConfig config = ZIMUserStatusSubscribeConfig();
config.subscriptionDuration = 60; // Subscription duration in minutes, valid range is 1 to 43200 (30 days)
// Call the subscribeUsersStatus method
ZIM.getInstance()?.subscribeUsersStatus(["userIdA","userIdB"], config).then((ZIMUsersStatusSubscribedResult result) {
for(ZIMErrorUserInfo errorUserInfo in result.errorUserList){
errorUserInfo.userID; // User ID failed to subscribe
errorUserInfo.reason; // Failure reason
}
}).catchError((onError){
});
1
Event notification
Subsequently, the online status changes of the target users within the specified subscriptionDuration
will be returned through the userStatusUpdated event callback registered by the ZIMEventHandler method.
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.
zim.setEventHandler(new ZIMEventHandler(){
public void onUserStatusUpdated(ZIM zim, ArrayList<ZIMUserStatus> userStatusList) {
for (ZIMUserStatus userStatus : userStatusList) {
String userID = userStatus.userID; // Target user ID
String onlineStatus = userStatus.onlineStatus; // Target user's online status
ArrayList<String> onlinePlatforms = userStatus.onlinePlatforms; // User online platforms list
long lastUpdateTime = userStatus.lastUpdateTime; // Last update time of online status
}
}
});
1
@method ZIMEventHandlerImpl : NSObject<ZIMEventHandler>
+(ZIMEventHandlerImpl *)getInstance();
@end
@implementation ZIMEventHandlerImpl
- (void)zim:(ZIM *)zim userStatusUpdated:(NSArray<ZIMUserStatus *> *)userStatusList {
for(ZIMUserStatus *userStatus in 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
}
}
// Other callback events..
@end
ZIMEventHandlerImpl *eventHandlerImpl = [ZIMEventHandlerImpl getInstance];
[zim setEventHandler: eventHandlerImpl];
1
@method ZIMEventHandlerImpl : NSObject<ZIMEventHandler>
+(ZIMEventHandlerImpl *)getInstance();
@end
@implementation ZIMEventHandlerImpl
- (void)zim:(ZIM *)zim userStatusUpdated:(NSArray<ZIMUserStatus *> *)userStatusList {
for(ZIMUserStatus *userStatus in 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
}
}
// Other callback events..
@end
ZIMEventHandlerImpl *eventHandlerImpl = [ZIMEventHandlerImpl getInstance];
[zim setEventHandler: eventHandlerImpl];
1
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
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
// Listen for the user status update event
zim.on('userStatusUpdated', function (zim, { userStatusList }) {
userStatusList.forEach((userStatus) => {
const { userID, onlineStatus, onlinePlatforms, lastUpdateTime } = userStatus;
console.log('userStatusUpdated:', userID, onlineStatus, onlinePlatforms, lastUpdateTime);
// Handle the logic after user status update here, such as updating UI or other operations
});
});
1
ZIMEventHandler.onUserStatusUpdated = (ZIM zim, List<ZIMUserStatus> userStatusList){
for (ZIMUserStatus userStatus in 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
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).
ArrayList<String> userIDs = new ArrayList<>();
userIDs.add("userIdA");
userIDs.add("userIdB");
zim.unsubscribeUsersStatus(userIDs, new ZIMUsersStatusUnsubscribedCallback() {
@Override
public void onUsersStatusUnsubscribed(ArrayList<ZIMErrorUserInfo> errorUserList, ZIMError errorInfo) {
for (ZIMErrorUserInfo errorUserInfo : errorUserList) {
System.out.println("User ID failed to unsubscribe: " + errorUserInfo.userID);
System.out.println("Failure reason: " + errorUserInfo.reason);
}
}
});
1
[zim unsubscribeUserStatus:@[@"userIdA",@"userIdB"]
callback:^(NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,
ZIMError *_Nonnull errorInfo) {
for(ZIMErrorUserInfo *errorUserInfo in errorUserList){
errorUserInfo.userID; // User ID of the failed unsubscription
errorUserInfo.reason; // Error code of the failed unsubscription
}
}];
1
[zim unsubscribeUserStatus:@[@"userIdA",@"userIdB"]
callback:^(NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,
ZIMError *_Nonnull errorInfo) {
for(ZIMErrorUserInfo *errorUserInfo in errorUserList){
errorUserInfo.userID; // User ID of the failed unsubscription
errorUserInfo.reason; // Error code of the failed unsubscription
}
}];
1
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
// List of user IDs to unsubscribe
const userIDs = ['userIdA', 'userIdB'];
try {
// Call the unsubscribeUsersStatus method
const result = await zim.unsubscribeUsersStatus(userIDs);
// Check for users who could not be unsubscribed
if (result.errorUserList.length > 0) {
result.errorUserList.forEach((errorUserInfo) => {
console.log('User ID of failed unsubscription: ', errorUserInfo.userID);
console.log('Error code of failed unsubscription: ', errorUserInfo.reason);
});
} else {
console.log('Successfully unsubscribe all users');
}
} catch (error) {
// Catch the exception and handle the error
console.error('An error occurred while unsubscribing:', error);
}
1
ZIM.getInstance()?.unsubscribeUsersStatus(["userIdA","userIdB"]).then((ZIMUsersStatusUnsubscribedResult result) {
for(ZIMErrorUserInfo errorUserInfo in result.errorUserList){
errorUserInfo.userID; // User ID failed to be unsubscribed
errorUserInfo.reason; // Failure reason
}
}).catchError((onError){
});
1
Query user online statuses
If you only need to get the online statuses of target users once, you can call the queryUsersStatusByUserIDs method and pass the target user IDs (up to 200 users, excluding the current user).
ArrayList<String> userIDs = new ArrayList<>();
userIDs.add("userIdA");
userIDs.add("userIdB");
zim.queryUsersStatus(userIDs, new ZIMUsersStatusQueriedCallback() {
@Override
public void onUsersStatusQueried(ArrayList<ZIMUserStatus> userStatusList,
ArrayList<ZIMErrorUserInfo> errorUserList, ZIMError errorInfo) {
for (ZIMUserStatus userStatus : userStatusList) {
System.out.println("User ID: " + userStatus.userID); // Target user ID
System.out.println("Online Status: " + userStatus.onlineStatus); // User online status
System.out.println("Online Platforms: " + userStatus.onlineStatus); // User online platforms list
System.out.println("Last Update Time: " + userStatus.lastUpdateTime); // Last update time of online status
}
for (ZIMErrorUserInfo errorUserInfo : errorUserList) {
System.out.println("Failed user ID: " + errorUserInfo.userID);
System.out.println("Failed error code: " + errorUserInfo.reason);
}
}
});
1
[zim queryUsersStatusByUserIDs:userIDs
callback:^(NSArray<ZIMUserStatus *> *_Nonnull userStatusList,
NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,
ZIMError *_Nonnull errorInfo) {
for(ZIMUserStatus *userStatus in 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(ZIMErrorUserInfo *errorUserInfo in errorUserList){
errorUserInfo.userID; // User ID of the failed query
errorUserInfo.reason; // Error code of the failed query
}
}];
1
[zim queryUsersStatusByUserIDs:userIDs
callback:^(NSArray<ZIMUserStatus *> *_Nonnull userStatusList,
NSArray<ZIMErrorUserInfo *> *_Nonnull errorUserList,
ZIMError *_Nonnull errorInfo) {
for(ZIMUserStatus *userStatus in 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(ZIMErrorUserInfo *errorUserInfo in errorUserList){
errorUserInfo.userID; // User ID of the failed query
errorUserInfo.reason; // Error code of the failed query
}
}];
1
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
// User ID list for querying status
const userIDs = ['userIdA', 'userIdB'];
try {
// Call the method to query user status
const result = await zim.queryUsersStatus(userIDs);
// Traverse the successfully queried user statuses
result.userStatusList.forEach((userStatus) => {
console.log('User ID:', userStatus.userID); // Target user ID
console.log('Online Status:', userStatus.onlineStatus); // User online status
console.log('Online Platforms:', userStatus.onlinePlatforms); // List of user online platforms
console.log('Last Update Time:', userStatus.lastUpdateTime); // Last update time of online status
});
// Check for failed user queries
result.errorUserList.forEach((errorUserInfo) => {
console.log('Failed User ID:', errorUserInfo.userID);
console.log('Error Code for Failed Query:', errorUserInfo.reason);
});
} catch (error) {
// Catch and handle errors
console.error('Error occurred while querying user status:', error);
}
1
ZIM.getInstance()?.queryUsersStatus(["userIdA","userIdB"]).then((ZIMUsersStatusQueriedResult result) {
for (ZIMUserStatus userStatus in result.userStatusList) {
userStatus.userID; // Target user ID
userStatus.onlineStatus; // User online status
userStatus.onlinePlatforms; // User online platform list
userStatus.lastUpdateTime; // Last update time of online status
}
for (ZIMErrorUserInfo errorUserInfo in result.errorUserList){
errorUserInfo.userID; // User ID failed to be queried
errorUserInfo.reason; // Failure reason
}
}).catchError((onError){
});
1
Query the online status subscription list
If users want to know their own online status subscription list, they can use the querySubscribedUserStatusListWithConfig 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.
// 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
ZIMSubscribedUserStatusQueryConfig config = new ZIMSubscribedUserStatusQueryConfig();
config.userIDs.add("userIdA");
config.userIDs.add("userIdB");
zim.querySubscribedUserStatusList(config, new ZIMSubscribedUserStatusListQueriedCallback() {
@Override
public void onSubscribedUserStatusListQueried(ArrayList<ZIMUserStatusSubscription> userStatusSubscriptionList, ZIMError errorInfo) {
for (ZIMUserStatusSubscription subscription : userStatusSubscriptionList) {
subscription.subscribeExpiredTime; // The subscription expiration timestamp of the user in the subscription list
subscription.userStatus; // The status information of the user in the subscription list
}
}
});
1
ZIMSubscribedUserStatusQueryConfig *config = [[ZIMSubscribedUserStatusQueryConfig alloc] init];
// 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 callback will include their status information
// If they are not, the result callback will not include any related information
config.userIDs = @[@"userIdA",@"userIdB"];
[zim
querySubscribedUserStatusListWithConfig:config
callback:^(NSArray<ZIMUserStatusSubscription *>
*_Nonnull userStatusSubscriptionList,
ZIMError *_Nonnull errorInfo) {
}];
1
ZIMSubscribedUserStatusQueryConfig *config = [[ZIMSubscribedUserStatusQueryConfig alloc] init];
// 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 callback will include their status information
// If they are not, the result callback will not include any related information
config.userIDs = @[@"userIdA",@"userIdB"];
[zim
querySubscribedUserStatusListWithConfig:config
callback:^(NSArray<ZIMUserStatusSubscription *>
*_Nonnull userStatusSubscriptionList,
ZIMError *_Nonnull errorInfo) {
}];
1
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
// Query configuration object
const config = {
userIDs: ['userIdA', 'userIdB'], // If empty, query all subscribed users
};
try {
// Call the method to query the subscribed user status list
const result = await zim.querySubscribedUserStatusList(config);
// Traverse the subscribed user status information
result.userStatusSubscriptionList.forEach((subscription) => {
console.log('Subscription expiration time:', subscription.subscribeExpiredTime); // Subscription expiration timestamp
console.log('User status information:', subscription.userStatus); // User status information
});
} catch (error) {
// Catch the exception and handle the error
console.error('An error occurred while querying the subscribed user status list:', error);
}
1
ZIMSubscribedUserStatusQueryConfig queryConfig = ZIMSubscribedUserStatusQueryConfig();
queryConfig.userIDs = ["userIdA","userIdB"];
// Query target user IDs (up to 200 users per query)
// When userIDs is empty, the method will get the complete subscription table information
// When userIDs is not empty, the method will check whether the target users are in the subscription list
// If the users exist, the result callback will present the user's status information
// If the user does not exist, the result callback will not include any relevant information
ZIM.getInstance()?.querySubscribedUserStatusList(queryConfig).then((ZIMSubscribedUserStatusListQueriedResult result) {
}).catchError((onError){
});
1
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 connectionStateChanged 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, ZIMConnectionStateConnected
means the user is online, ZIMConnectionStateDisconnected
means the user is offline. The remaining ZIMConnectionStateConnecting
and ZIMConnectionStateReconnecting
connection states can be determined as either online or offline based on your business logic.
zim.setEventHandler(new ZIMEventHandler(){
public void onConnectionStateChanged(ZIM zim, ZIMConnectionState state,
ZIMConnectionEvent event, JSONObject extendedData) {
switch (state) {
case CONNECTED:
// Connected, you can map the current user's status as online
break;
case CONNECTING:
// Connecting, you can map the current user's status as online or offline based on your business logic
break;
case RECONNECTING:
// Reconnecting, you can map the current user's status as online or offline based on your business logic
break;
case DISCONNECTED:
// Disconnected, you can map the current user's status as offline
break;
default:
break;
}
}
});
1
@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
@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
@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
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
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
zim.on('connectionStateChanged', function (zim, { state, event }) {
switch (state) {
case 2: // Connected
console.log('Connected');
break;
case 1: // Connecting:
console.log('Connecting');
break;
case 3: // Reconnecting:
console.log('Reconnecting');
break;
case 0: // Disconnected:
console.log('Disconnected');
break;
default:
console.log('Unknown');
break;
}
console.log('Connection event:', event, state);
});
1
ZIMEventHandler.onConnectionStateChanged = (ZIM zim, ZIMConnectionState state,
ZIMConnectionEvent event, Map extendedData){
switch (state) {
case ZIMConnectionState.connected:
break;
case ZIMConnectionState.connecting:
break;
case ZIMConnectionState.reconnecting:
break;
case ZIMConnectionState.disconnected:
break;
default:
break;
}
};
1
In the multi-login scenario, when a user logs in to a second platform, you can listen to the userStatusUpdated 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).
When the userStatusUpdated 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.
zim.setEventHandler(new ZIMEventHandler(){
public void onUserStatusUpdated(ZIM zim, ArrayList<ZIMUserStatus> userStatusList) {
String myUserID = "Current User ID"; // The current user ID
for (ZIMUserStatus userStatus : userStatusList) {
if (userStatus.userID.equals(myUserID)) {
// If it is the current user, onlineStatus will be UNKNOWN
ZIMUserOnlineStatus onlineStatus = userStatus.onlineStatus;
// Online platforms list of the current user
ArrayList<ZIMPlatformType> onlinePlatforms = userStatus.onlinePlatforms;
// If it is the current user, lastUpdateTime will be 0
long lastUpdateTime = userStatus.lastUpdateTime;
}
}
}
});
1
@interface ZIMEventHandlerImpl : NSObject<ZIMEventHandler>
+(ZIMEventHandlerImpl *)getInstance();
@end
@implementation ZIMEventHandlerImpl
- (void)zim:(ZIM *)zim userStatusUpdated:(NSArray<ZIMUserStatus *> *)userStatusList {
for(ZIMUserStatus *userStatus in userStatusList){
if([userStatus.userID isEqual:myUserID]){
userStatus.onlineStatus; // If it is the current user, onlineStatus will be unknown
userStatus.onlinePlatforms; // Online platforms list of the current user
userStatus.lastUpdateTime; // If it is the current user, lastUpdateTime will be 0
}
}
}
// Other callback events..
@end
ZIMEventHandlerImpl *eventHandlerImpl = [ZIMEventHandlerImpl getInstance];
[zim setEventHandler: eventHandlerImpl];
1
@interface ZIMEventHandlerImpl : NSObject<ZIMEventHandler>
+(ZIMEventHandlerImpl *)getInstance();
@end
@implementation ZIMEventHandlerImpl
- (void)zim:(ZIM *)zim userStatusUpdated:(NSArray<ZIMUserStatus *> *)userStatusList {
for(ZIMUserStatus *userStatus in userStatusList){
if([userStatus.userID isEqual:myUserID]){
userStatus.onlineStatus; // If it is the current user, onlineStatus will be unknown
userStatus.onlinePlatforms; // Online platforms list of the current user
userStatus.lastUpdateTime; // If it is the current user, lastUpdateTime will be 0
}
}
}
// Other callback events..
@end
ZIMEventHandlerImpl *eventHandlerImpl = [ZIMEventHandlerImpl getInstance];
[zim setEventHandler: eventHandlerImpl];
1
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
zim.on('userStatusUpdated', function (zim, userStatusList) {
const myUserID = 'current user ID'; // Replace with the actual ID of the current user
userStatusList.forEach((userStatus) => {
if (userStatus.userID === myUserID) {
// If it is the current user, onlineStatus will be unknown and cannot accurately reflect the current user's online status
const onlineStatus = userStatus.onlineStatus;
// List of online platforms for the current user
const onlinePlatforms = userStatus.onlinePlatforms;
// If it is the current user, lastUpdateTime will be 0
const lastUpdateTime = userStatus.lastUpdateTime;
// Handle logic for online status, online platforms, etc.
console.log('Current user online status:', onlineStatus);
console.log('Current user online platforms:', onlinePlatforms);
console.log('Last status update time:', lastUpdateTime);
}
});
});
1
ZIMEventHandler.onUserStatusUpdated = (ZIM zim, List<ZIMUserStatus> userStatusList){
for (ZIMUserStatus userStatus in userStatusList) {
if (userStatus.userID == "myUserID"){
userStatus.onlineStatus; // If it is the current user, onlineStatus will be unknown and cannot accurately reflect the current user's online status
userStatus.onlinePlatforms; // List of online platforms for the current user
userStatus.lastUpdateTime; // If it is the current user, lastUpdateTime will be 0
}
}
};
1