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 Presence
, PresenceManager
, and IPresenceManagerDelegate
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 PresenceManager#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 IPresenceManagerDelegate#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:
SDKClient.Instance.PresenceManager.SubscribePresences(members, expiry, new ValueCallBack<List<Presence>>(
onSuccess: (list) =>
{
foreach (var it in list)
{
}
},
onError: (code, desc) =>
{
Debug.Log($"SubscribePresences failed, code:{code}, desc:{desc}");
}
));
You can call PresenceManager#PublishPresence
to publish a custom status. Whenever your presence status updates, the users who subscribe to you receive the IPresenceManagerDelegate#OnPresenceUpdated
callback.
The following code sample shows how to publish a custom status:
SDKClient.Instance.PresenceManager.PublishPresence(ext, new CallBack(
onSuccess: () => {
Debug.Log($"PublishPresence success.");
},
onError: (code, desc) => {
Debug.Log($"PublishPresence failed, code:{code}, desc:{desc}");
}
));
Refer to the following code sample to listen for presence status updates:
PresenceM
// Adds the presence status listener.anagerDelegate presenceManagerDelegate = new PresenceManagerDelegate();
SDKClient.Instance.PresenceManager.AddPresenceManagerDelegate(presenceManagerDelegate);
// Occurs when the presence statuses of the subscriptions update.
public interface IPresenceManagerDelegate
{
void OnPresenceUpdated(List<Presence> presences);
}
You can call PresenceManager#UnsubscribePresences
to unsubscribe from the presence statuses of the specified users, as shown in the following code sample:
SDKClient.Instance.PresenceManager.UnsubscribePresences(mem_list, new CallBack(
onSuccess: () => {
Debug.Log($"UnsubscribePresences success.");
},
onError: (code, desc) => {
Debug.Log($"UnsubscribePresences failed, code:{code}, desc:{desc}");
}
));
You can call PresenceManager#FetchSubscribedMembers
to retrieve the list of your subscriptions in a paginated list, as shown in the following code sample:
SDKClient.Instance.PresenceManager.FetchSubscribedMembers(pageNum, pageSize, new ValueCallBack<List<string>>(
onSuccess: (list) =>
{
},
onError: (code, desc) =>
{
Debug.Log($"SubscribePresences failed, code:{code}, desc:{desc}");
}
));
You can call PresenceManager#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:
SDKClient.Instance.PresenceManager.FetchPresenceStatus(members, new ValueCallBack<List<Presence>>(
onSuccess: (list) =>
{
foreach (var it in list)
{
}
},
onError: (code, desc) =>
{
Debug.Log($"FetchPresenceStatus failed, code:{code}, desc:{desc}");
}
));