Skip to main content

Subscribe to online status

This guide explains how to subscribe to, query, and listen for user connection status changes using the Nexconn SDK for Android.

Listen for subscription events

To receive real-time presence change notifications, use NCEngine.addUserHandler().

When handling subscription events, check the SubscribeType in the callback. For presence subscriptions, the type is SubscribeType.ONLINE_STATUS.

kotlin
NCEngine.addUserHandler("PRESENCE_HANDLER", object : UserHandler {
override fun onSubscriptionChanged(event: SubscriptionChangedEvent) {
if (event.subscribeType == SubscribeType.ONLINE_STATUS) {
event.events.forEach { info ->
val isOnline = info.details.any { it.eventValue == 1 }
println("Presence changed for user: ${info.userId}, online: $isOnline")
}
}
}

override fun onSubscriptionSyncCompleted(event: SubscriptionSyncCompletedEvent) {
if (event.subscribeType == SubscribeType.ONLINE_STATUS) {
// Presence subscription sync complete
}
}

override fun onSubscriptionChangedOnOtherDevices(event: SubscriptionChangedOnOtherDevicesEvent) {
event.events.forEach { info ->
println("Subscription changed on other device: ${info.userId}")
}
}
})

Remove the listener

kotlin
NCEngine.removeUserHandler("PRESENCE_HANDLER")

Subscribe to presence

Use NCEngine.userModule.subscribeEvent() with SubscribeEventParams to track specific users' connection status.

tip
  • Up to 200 users per subscription call.
  • Maximum total subscriptions: 1,000 users.
  • Subscription duration: 60 to 2,592,000 seconds.
  • Each user can be subscribed to by up to 5,000 users.
kotlin
val params = SubscribeEventParams(
subscribeType = SubscribeType.ONLINE_STATUS,
expiry = 180000,
userIds = listOf("user1", "user2", "user3")
)

NCEngine.userModule.subscribeEvent(params) { failedIds, error ->
if (error == null) {
// Subscribed successfully
} else {
// failedIds contains user IDs that failed
}
}

Query subscribed users' presence

Use NCEngine.userModule.getSubscribeUsersOnlineStatus() to query the current presence of subscribed users in bulk.

kotlin
val userIds = listOf("user1", "user2", "user3")

NCEngine.userModule.getSubscribeUsersOnlineStatus(userIds) { statuses, error ->
if (error == null && statuses != null) {
statuses.forEach { status ->
println("User: ${status.userId}, Online: ${status.isOnline}")
}
}
}

Unsubscribe from presence

Use NCEngine.userModule.unsubscribeEvent() to stop tracking a user's connection status.

kotlin
val params = UnsubscribeEventParams(
subscribeType = SubscribeType.ONLINE_STATUS,
userIds = listOf("user1", "user2", "user3")
)

NCEngine.userModule.unsubscribeEvent(params) { failedIds, error ->
if (error == null) {
// Unsubscribed successfully
}
}

Query subscription status for specific users

Use NCEngine.userModule.getSubscribeEvent() to check the subscription status of specific users. You can query up to 200 users at a time.

kotlin
val params = GetSubscribeEventParams(
subscribeType = SubscribeType.ONLINE_STATUS,
userIds = listOf("user1", "user2", "user3")
)

NCEngine.userModule.getSubscribeEvent(params) { subscribeInfoList, error ->
if (error == null && subscribeInfoList != null) {
subscribeInfoList.forEach { info ->
println("Subscribed user: ${info.userId}, expires: ${info.expiry}")
}
}
}

List subscribed users (paginated)

Use NCEngine.userModule.createSubscribeQuery() with SubscribeQueryParams to retrieve all subscribed users with pagination.

kotlin
val params = SubscribeQueryParams(SubscribeType.ONLINE_STATUS).apply {
pageSize = 20
}

val query = NCEngine.userModule.createSubscribeQuery(params)

query.loadNextPage { page, error ->
if (error == null && page != null) {
page.data.forEach { info ->
println("Subscribed user: ${info.userId}")
}
if (page.hasMore) {
// Fetch next page
}
}
}