Skip to main content
Android
iOS
Web
Windows
Unity
Flutter
React Native

Presence

The presence feature enables users to publicly display their online presence status and quickly determine the status of other users. Users can also customize their presence status, which adds fun and diversity to real-time chatting.

The following illustration shows the implementation of creating a custom presence status and how various presence statues look in a contact list:

1655302046418

This page shows how to use the Chat SDK to implement presence in your project.

Understand the tech

The Chat SDK provides the Presence, PresenceManager, and PresenceListener classes for presence management, which allows you to implement the following features:

  • Subscribe to the presence status of one or more users
  • Unsubscribe from the presence status of one or more users
  • Listen for presence status updates
  • Publish a custom presence status
  • Retrieve the list of subscriptions
  • Retrieve the presence status of one or more users

The following figure shows the workflow of how clients subscribe to and publish presence statuses:

1655306619037

As shown in the figure, the workflow of presence subscription and publication is as follows:

  1. User A subscribes to the presence status of User B.
  2. User B publishes a presence status.
  3. The Chat server triggers an event to notify User A about the presence update of User B.

Prerequisites

Before proceeding, ensure that your environment meets the following requirements:

  • You have initialized the Chat SDK. For details, see SDK quickstart.
  • You understand the API call frequency limit as described in Limitations.
  • You have activated the presence feature in Agora Console.

Implementation

This section introduces how to implement presence functionalities in your project.

Subscribe to the presence status of one or more users

By default, you do not subscribe to any users. To subscribe to the presence statuses of the specified users, you can call subscribePresences.

Once the subscription succeeds, the onSuccess callback is triggered, notifying you about the current statuses of the specified users synchronously. Whenever the specified users update their presence statuses, the onPresenceUpdated callback is triggered, notifying you about the updated statuses asynchronously.

The following code sample shows how to subscribe to the presence status of one or more users:


_10
ChatClient.getInstance().presenceManager().subscribePresences(contactsFromServer, 1 * 24 * 3600, new ValueCallBack<List<Presence>>() {
_10
@Override
_10
public void onSuccess(List<Presence> presences) {
_10
_10
}
_10
@Override
_10
public void onError(int errorCode, String errorMsg) {
_10
_10
}
_10
});

  1. You can subscribe to a maximum of 100 users at each call. The total subscriptions of each user cannot exceed 3,000. Once the number of subscriptions exceed the limit, the subsequent subscriptions with longer durations succeed and replace the existing subscriptions with shorter durations.
  2. The subscription duration can be a maximum of 30 days. When the subscription to a user expires, you need to subscribe to this user once again. If you subscribe to a user again before the current subscription expires, the duration is reset.

Publish a custom presence status

You can call publishPresence to publish a custom status. Whenever your presence status updates, the users who subscribe to you receive the onPresenceUpdated callback.

The following code sample shows how to publish a custom status:


_8
ChatClient.getInstance().presenceManager().publishPresence("custom status", new CallBack() {
_8
@Override
_8
public void onSuccess() {
_8
}
_8
@Override
_8
public void onError(int code, String error) {
_8
}
_8
});

Listen for presence status updates

Refer to the following code sample to listen for presence status updates:


_7
// Adds the presence status listener.
_7
ChatClient.getInstance().presenceManager().addListener(new MyPresenceListener());
_7
_7
// Occurs when the presence statuses of the subscriptions update.
_7
public interface PresenceListener {
_7
void onPresenceUpdated(List<Presence> presences);
_7
}

Unsubscribe from the presence status of one or more users

You can call unsubscribePresences to unsubscribe from the presence statuses of the specified users, as shown in the following code sample:


_10
ChatClient.getInstance().presenceManager().unsubscribePresences(contactsFromServer, new CallBack() {
_10
@Override
_10
public void onSuccess() {
_10
_10
}
_10
@Override
_10
public void onError(int errorCode, String errorMsg) {
_10
_10
}
_10
});

Retrieve the list of subscriptions

You can call fetchSubscribedMembers to retrieve the list of your subscriptions in a paginated list, as shown in the following code sample:


_10
ChatClient.getInstance().presenceManager().fetchSubscribedMembers(1, 50, new ValueCallBack<List<String>>() {
_10
@Override
_10
public void onSuccess(List<String> subscribedMembers) {
_10
_10
}
_10
@Override
_10
public void onError(int errorCode, String errorMsg) {
_10
_10
}
_10
});

Retrieve the presence status of one or more users

You can call fetchPresenceStatus to retrieve the current presence statuses of the specified users without the need to subscribe to them, as shown in the following code sample:


_12
// contactsFromServer: The ID list of users whose presence status you retrieve.
_12
// You can pass in up to 100 user IDs.
_12
ChatClient.getInstance().presenceManager().fetchPresenceStatus(contactsFromServer, new ValueCallBack<List<Presence>>() {
_12
@Override
_12
public void onSuccess(List<Presence> presences) {
_12
_12
}
_12
@Override
_12
public void onError(int errorCode, String errorMsg) {
_12
_12
}
_12
});

vundefined