Skip to main content

Subscribe to online status

Subscribe to other users' online/offline status changes and receive real-time notifications when their status changes.

Subscribe to a user's online status

TypeScript
import { NCEngine, SubscribeType } from '@nexconn/chat';

const { code } = await NCEngine.userModule.subscribeEvent({
userIds: ['user1', 'user2', 'user3'],
subscribeType: SubscribeType.ONLINE_STATUS,
});
if (code === 0) {
console.log('Subscribed to online status');
}

Parameters

ParameterTypeRequiredDescription
userIdsstring[]YesArray of user IDs to subscribe to
subscribeTypeSubscribeTypeYesSubscription type. Use SubscribeType.ONLINE_STATUS for online status.
expirynumberNoSubscription validity period in seconds. Omit for non-expiring subscriptions.

Listen for online status changes

Register a UserHandler to receive online status events:

TypeScript
import {
NCEngine,
UserHandler,
SubscriptionChangedEvent,
SubscriptionSyncCompletedEvent,
SubscriptionChangedOnOtherDevicesEvent,
} from '@nexconn/chat';

NCEngine.addUserHandler('online-status-handler', new UserHandler({
onSubscriptionChanged(event: SubscriptionChangedEvent): void {
event.events.forEach((item) => {
console.log('User status changed:', item);
});
},
onSubscriptionSyncCompleted(event: SubscriptionSyncCompletedEvent): void {
console.log('Subscription sync completed for type:', event.type);
},
onSubscriptionChangedOnOtherDevices(event: SubscribeChangedOnOtherDevicesEvent): void {
event.events.forEach((item) => {
console.log('Subscription changed on another device:', item);
});
},
}));

Remove the handler when it is no longer needed:

TypeScript
NCEngine.removeUserHandler('online-status-handler');

Unsubscribe

TypeScript
const { code } = await NCEngine.userModule.unsubscribeEvent({
userIds: ['user1', 'user2'],
subscribeType: SubscribeType.ONLINE_STATUS,
});
info
  • You can subscribe to a limited number of users. Check the API reference for the maximum subscription count.
  • Online status subscriptions may require the feature to be enabled on the Console.