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:
This page shows how to use the Agora Chat SDK to implement presence in your project.
The Agora Chat SDK provides the ChatPresence
, ChatPresenceManager
, and ChatPresenceEventHandler
classes for presence management, which allows you to implement the following features:
The following figure shows the workflow of how clients subscribe to and publish presence statuses:
As shown in the figure, the workflow of presence subscription and publication is as follows:
Before proceeding, ensure that your environment meets the following requirements:
This section introduces how to implement presence functionalities in your project.
By default, you do not subscribe to any users. To subscribe to the presence statuses of the specified users, you can call subscribe
. Whenever the specified users update their presence statuses, the onPresenceStatusChanged
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:
// members: The ID list of users to whom you subscribe.
List<String> members = [];
// expiry: The subscription duration in seconds.
int expiry = 100;
try {
List<ChatPresence> list =
await ChatClient.getInstance.presenceManager.subscribe(
members: members,
expiry: expiry,
);
} on ChatError catch (e) {
}
You can call publishPresence
to to publish a custom status. Whenever your presence status updates, the users who subscribe to you receive the ChatPresenceEventHandler#onPresenceStatusChanged
callback.
The following code sample shows how to publish a custom status:
try {
// description: The custom presence status.
await ChatClient.getInstance.presenceManager.publishPresence(description);
} on ChatError catch (e) {
}
Refer to the following code sample to listen for presence status updates:
// Adds the presence event handler.
ChatClient.getInstance.presenceManager.addEventHandler(
"UNIQUE_HANDLER_ID",
ChatPresenceEventHandler(
onPresenceStatusChanged: (list) {},
),
);
...
// Removes the presence event handler.
ChatClient.getInstance.presenceManager.removeEventHandler("UNIQUE_HANDLER_ID");
You can call unSubscribe
to unsubscribe from the presence statuses of the specified users, as shown in the following code sample:
// memberIds: The ID list of users from whom you unsubscribe.
try {
await ChatClient.getInstance.presenceManager.unSubscribe(
members: members,
);
} on ChatError catch (e) {
}
You can call fetchSubscribedMembers
to retrieve the list of your subscriptions in a paginated list, as shown in the following code sample:
try {
List<String> subMembers =
await ChatClient.getInstance.presenceManager.fetchSubscribedMembers();
} on ChatError catch (e) {
}
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:
// memberIds: The ID list of users whose presence statuses you retrieve.
try {
List<ChatPresence> list = await ChatClient.getInstance.presenceManager
.fetchPresenceStatus(members: memberIds);
} on ChatError catch (e) {
}