Presence
Track which users are online and active in your chat app. Display user status (online, offline, active, away) and show when users were last active.
The Chat SDK provides two types of presence tracking:
| Type | Description | Data source |
|---|---|---|
| Channel presence | Real-time tracking of users subscribed to specific channels | Presence API |
| Global presence | App-wide activity tracking based on timestamps | LastActiveTimeStamp property |
Channel presence provides real-time updates through the Presence API. Use StreamPresence() to track who connects or disconnects.
Global presence uses the LastActiveTimeStamp property on User objects. Configure the update interval (default: 10 minutes, minimum: 1 minute) during initialization with StoreUserActivityTimestamps.
Channel presence
These methods let you monitor who is subscribed to a given channel ("present" on that channel).
Requires Presence
All channel presence methods in this section require that Presence is enabled for your app's keyset in the Admin Portal.
You can retrieve similar information for presence with different methods by calling them on the User, Channel, or Chat object. Depending on the chosen method, you must provide a different input information set.
Return channels where user is present
You can return a list of channels where a given user is present with:
WherePresent()called on theUserobjectWherePresent()called on theChatobject.
Both of these methods have the same name and give the same output. The only difference is that you call a given method either on the Chat or the User object. Depending on the object, you either have to specify the ID of the user whose presence you want to check or not because it's already known.
Method signature
These methods take the following parameters:
-
WherePresent()(on theUserobject)1user.WherePresent() -
WherePresent()(on theChatobject)1chat.WherePresent(
2 string userId
3)
Input
| Parameter | Required in the User object method | Required in the Chat object method | Description |
|---|---|---|---|
userIdType: stringDefault: n/a | No | Yes | Unique identifier (up to 92 UTF-8 characters) of the user whose presence you want to check. |
Output
This method returns an awaitable Task<ChatOperationResult<List<string>>> with all channel IDs on which the given user is present.
Sample code
Get a list of channels on which the support_agent_15 user is present.
-
WherePresent()(on theUserobject)1 -
WherePresent()(on theChatobject)1
Check user's channel presence
You can return information if the user is present on a specified channel with:
IsPresentOn()called on theUserobjectIsUserPresent()called on theChannelobject.IsPresent()called on theChatobject.
All of these methods give the same output. The only difference is that you call a given method on the User, Channel, or Chat object. Depending on the object, you have to specify the ID of the user whose presence you want to check, the channel ID where you want to check user's presence, or both user and channel IDs.
Method signature
These methods take the following parameters:
-
IsPresentOn()(on theUserobject)1user.IsPresentOn(
2 string channelId
3) -
IsUserPresent()(on theChannelobject)1channel.IsUserPresent(
2 string userId
3) -
IsPresent()(on theChatobject)1chat.IsPresent(
2 string userId,
3 string channelId
4)
Input
| Parameter | Required in the User object method | Required in the Channel object method | Required in the Chat object method | Description |
|---|---|---|---|---|
userIdType: stringDefault: n/a | No | Yes | Yes | Unique ID (up to 92 UTF-8 characters) of the user whose presence you want to check. |
channelIdType: stringDefault: n/a | Yes | No | Yes | Unique identifier of the channel where you want to check the user's presence. |
Output
Returns an awaitable Task<ChatOperationResult<bool>> information on whether a given user is present on a specified channel (true) or not (false).
Sample code
Find out if the support_agent_15 user is present on the support channel.
-
IsPresentOn()(on theUserobject)1 -
IsUserPresent()(on theChannelobject)1 -
IsPresent()(on theChatobject)1
Return all users present on channel
You can return a list of users present on the given channel with:
WhoIsPresent()called on theChannelobjectWhoIsPresent()called on theChatobject.
Both of these methods have the same name and give the same output. The only difference is that you call a given method either on the Chat or the Channel object. Depending on the object, you either have to specify the ID of the channel where you want to check all present users or not because it's already known.
Method signature
These methods take the following parameters:
-
WhoIsPresent()(on theChannelobject)1channel.WhoIsPresent(
2 int limit = 1000,
3 int offset = 0
4) -
WhoIsPresent()(on theChatobject)1chat.WhoIsPresent(
2 string channelId
3)
Input
| Parameter | Required in the Channel object method | Required in the Chat object method | Description |
|---|---|---|---|
channelIdType: stringDefault: n/a | No | Yes | Unique identifier of the channel where you want to check all present users. |
limitType: intDefault: 1000 | No | No | Maximum number of user details to return. The default and maximum value is 1000. |
offsetType: intDefault: 0 | No | No | Starting position of results for pagination purposes. Use this parameter to page through results when there are more users than the limit allows. |
Output
This method returns an awaitable Task<ChatOperationResult<List<string>>> with all user IDs on the channel.
Sample code
Get a list of users that are present on the support channel.
-
WhoIsPresent()(on theChannelobject)1 -
WhoIsPresent()(on theChatobject)1
Get presence updates
Get up-to-date information about the real-time presence of users in the specified channel by subscribing to Presence events.
Use the StreamPresence() method to enable or disable presence event streaming on a channel, and the OnPresenceUpdate event to handle presence updates. This lets you constantly track who connects to or disconnects from the channel and visually represent that in your chat app through some status, like Offline, Online, Active, Away, or any other.
Method naming
Earlier versions used SetListeningForPresence() to enable streaming. This method has been superseded by StreamPresence(), though it remains available for backward compatibility.
Method signature
These methods take the following parameters:
-
StreamPresence()1channel.StreamPresence(bool stream) -
OnPresenceUpdate- Event signature1// event on the Channel entity
2public event Action<List<string>> OnPresenceUpdate;
3// needs a corresponding event handler
4void EventHandler(List<string> users)
Input
| Parameter | Required in StreamPresence() | Required in OnPresenceUpdate | Description |
|---|---|---|---|
streamType: boolDefault: n/a | Yes | n/a | Whether to start (true) or stop (false) listening to presence events on the channel. |
usersType: List<string>Default: n/a | No | Yes | List of user IDs present on the channel. |
Output
These methods don't return a value. Presence updates are delivered through the OnPresenceUpdate event handler.
Sample code
Get user presence updates on support channel.
1
Global presence
The Unity Chat SDK lets you configure your app to track the user's last online activity - this gives near real-time visibility into the availability of other chat members, allowing you to see whether someone is offline or available and reach out to them to start immediate communication.
Using this online activity information provided by the Chat SDK, you can later configure your app to display different statuses next to user profiles, like Offline, Online, Active, Away or any other.
This feature relies on the LastActiveTimeStamp property set in milliseconds on the User object. This property stands for the Unix timestamp (numeric value representing the number of seconds since January 1, 1970) for the last time the user was active in a chat app. To track this, you must explicitly state that when configuring your app during the Unity Chat SDK initialization by:
- Setting the
StoreUserActivityTimestampsparameter totrue. - Deciding how frequently a user's online activity will be updated by configuring the
StoreUserActivityIntervaloption - the default value is set to600000milliseconds (10 minutes) and the minimum value is60000milliseconds (1 minute).
If you set these options, you can track a user's global presence by accessing the Active property on the User object that relies on the above setup. If the user showed no online activity within the defined period of time, they are considered inactive.
Check user's app presence
You can access the Active property of the User object to check whether a user has recently been active in the chat app based on their last activity timestamp and a configured interval.
Required configuration
To track the user's online activity, you must first configure the StoreUserActivityTimestamps and StoreUserActivityInterval parameters when initializing the Unity Chat SDK.
Method signature
This is how you can access the property:
1user.Active: bool
Properties
| Property | Description |
|---|---|
ActiveType: bool | Returned info on whether the user is active (true) or not active (false) on the channel. The returned value depends strictly on how you configure your chat app during initialization - if you set the storeUserActivityInterval parameter to the default 600000 milliseconds (10 minutes) and the user has been active in the app within the last 10 minutes (based on their LastActiveTimeStamp property), accessing the Active property returns true. |
Sample code
Check if the user support_agent_15 has been recently active (assuming you configured the chat app to use the default activity interval value).
1
Check user's last online activity
Required configuration
To track the user's online activity, you must first configure the StoreUserActivityTimestamps and StoreUserActivityInterval parameters when initializing the Chat SDK.
Let's assume you configured your app to track the user's online activity and update it every 2 minutes. You can retrieve information on the user's last online activity directly from the User object (LastActiveTimeStamp property), convert it to a human-readable date (using external date and time libraries), and display it next to the user's profile in your chat app. Thanks to that, other app users will be able to see the last time the given user was online.
Method signature
This is how you can access the property:
1user.LastActiveTimeStamp: string
Properties
| Property | Description |
|---|---|
LastActiveTimeStampType: string | Timestamp for the last time the user was active in a chat app. |
Sample code
Show the Unix timestamp when support_agent_15 was last time active in an app.
1