Skip to main content

You are viewing Agora Docs forBeta products and features. Switch to Docs

Presence

In real-time messaging solutions, it is often important to know whether a user is currently online or offline. For example, in instant messaging, chat applications, and online collaboration tools, users need to see the availability of their contacts. This information is typically displayed as a status message or icon next to a user's name. Presence features in Signaling SDK enable you to monitor join, leave, and status change notifications for users in a channel. Using Presence, you can:

  • Get a list of users currently in a channel and their temporary status data.
  • Get a list of channels a specified user has joined or is subscribed to.
  • Read, set, or remove user status.
  • Receive real-time event notifications when users join or leave specified channels.
  • Receive user status change event notifications in real time.

Understand the tech

Presence provides real-time information about the availability, and the current status of users, for effective communication and collaboration. It enables you to retrieve a list of users in a channel, or to query the list of channels for a specific user. The following figure illustrates how you integrate presence features into your app.

Prerequisites

To follow this page, you must have:

Implement presence

To implement presence,

Add the presence event listener


_12
protected open val eventListener: RtmEventListener = object : RtmEventListener {
_12
_12
override fun onPresenceEvent(eventArgs: PresenceEvent) {
_12
// Your Presence Event handler
_12
if (eventArgs.eventType == RtmConstants.RtmPresenceEventType.SNAPSHOT) {
_12
channelType = eventArgs.channelType
_12
}
_12
mListener?.onSignalingEvent("Presence", eventArgs)
_12
}
_12
_12
// Other event listeners
_12
}

Enable presence notifications when you subscribe to a channel


_21
fun subscribe(channelName: String): Int {
_21
// Subscribe to a channel
_21
val subscribeOptions = SubscribeOptions(
_21
true,
_21
true, // Subscribe with Presence
_21
true,
_21
true
_21
)
_21
signalingEngine?.subscribe(channelName, subscribeOptions, object: ResultCallback<Void?> {
_21
override fun onFailure(errorInfo: ErrorInfo?) {
_21
notify("Subscribe failed:\n"+ errorInfo.toString())
_21
}
_21
_21
override fun onSuccess(responseInfo: Void?) {
_21
isSubscribed = true
_21
mListener?.onSubscribeUnsubscribe(isSubscribed)
_21
notify("Subscribed to channel: $channelName")
_21
}
_21
})
_21
return 0
_21
}

Obtain a list of users in the channel

To get a list of users in a channel, call getOnlineUsers():


_15
fun getOnlineUsers () {
_15
_15
val getOnlineUsersOptions = GetOnlineUsersOptions(true, true)
_15
signalingEngine?.presence?.getOnlineUsers(channelName, channelType, getOnlineUsersOptions, object: ResultCallback<GetOnlineUsersResult?> {
_15
override fun onFailure(errorInfo: ErrorInfo?) {
_15
notify("Failed to obtain user list")
_15
}
_15
_15
override fun onSuccess(getOnlineUsersResult: GetOnlineUsersResult?) {
_15
val list = getOnlineUsersResult?.userStateList
_15
val userList: List<String> = list?.map { it.userId } ?: emptyList()
_15
mListener?.onUserListUpdated(userList)
_15
}
_15
})
_15
}

Set local user status

To set the local user status, call setState.


_15
fun setStatus() {
_15
val stateItems: ArrayList<StateItem> = ArrayList()
_15
stateItems.add(StateItem("mood", "pumped"))
_15
_15
signalingEngine?.presence?.setState(channelName, channelType, stateItems,
_15
object : ResultCallback<Void?> {
_15
override fun onSuccess(responseInfo: Void?) {
_15
notify("Set state success")
_15
}
_15
_15
override fun onFailure(errorInfo: ErrorInfo) {
_15
notify(errorInfo.toString())
_15
}
_15
})
_15
}

Get the status of a remote user

To read the status of a remote user, call getState.


_16
fun getState(uid: Int) {
_16
signalingEngine?.presence?.getState(channelName, channelType, uid.toString(),
_16
object : ResultCallback<UserState?> {
_16
override fun onSuccess(state: UserState?) {
_16
if (state != null) {
_16
for (item in state.states) {
_16
Log.i("getState $uid:", item.toString())
_16
}
_16
}
_16
}
_16
_16
override fun onFailure(errorInfo: ErrorInfo) {
_16
notify(errorInfo.toString())
_16
}
_16
})
_16
}

Test presence

This section explains how to run the Signaling reference app and test presence features. To run the project, take the following steps:

For each user in your tests:

  1. Configure the project

    1. Open the file <samples-root>/signaling-manager/src/main/res/raw/config.json

    2. Replace the value for appId with the value from Agora Console.
    3. Generate an RTM token using your uid.
    4. Replace the value for token with the Signaling token.
    5. Replace the value for uid with the the value you used to generate the token.
    6. Ensure that the channelName is filled in. The channel name can be any string.
  2. Run the reference app

    1. In Android Studio, connect a physical Android device to your development machine.
    2. Click Run to start the app.
    3. A moment later you see the project installed on your device.
  3. Test presence functionality:

    1. Run multiple instances of the SDK quickstart example.
    2. Log in to Signaling from each instance using a different user ID.
    3. Subscribe to the same channel from all instances. You see a list of all users in the channel.
    4. Unsubscribe a user from the channel. You see that the corresponding user ID disappears from the user list.

Reference

This section contains additional information that either supplements the content on this page or directs you to documentation that covers other aspects of this product.

Signaling