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(): Promise<string[]> -
wherePresent()(on theChatobject)1chat.wherePresent(
2 id: string
3): Promise<string[]>
Input
| Parameter | Required in the User object method | Required in the Chat object method | Description |
|---|---|---|---|
idType: stringDefault: n/a | No | Yes | Unique identifier (up to 92 UTF-8 characters) of the user whose presence you want to check. |
Output
| Type | Description |
|---|---|
Promise<string[]> | List of all channel IDs on which the given user is present. |
Errors
Whenever the user ID is required, and you try to check a user without providing their ID, you will receive the ID is required error.
Sample code
Get a list of channels on which the support_agent_15 user is present.
-
wherePresent()(on theUserobject)1// reference the user you want to check
2const user = await chat.getUser("support_agent_15")
3// invoke the "wherePresent()" method
4const channelIds = await user.wherePresent() -
wherePresent()(on theChatobject)1// reference the "chat" object and invoke the "wherePresent()" method.
2const channelIds = await chat.wherePresent(
3 "support_agent_15"
4)
Check user's channel presence
You can return information if the user is present on a specified channel with:
isPresentOn()called on theUserobjectisPresent()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 channelId: string
3): Promise<boolean> -
isPresent()(on theChannelobject)1channel.isPresent(
2 userId: string,
3): Promise<boolean> -
isPresent()(on theChatobject)1chat.isPresent(
2 userId: string,
3 channelId: string
4): Promise<boolean>
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
| Type | Description |
|---|---|
Promise<boolean> | Returns information on whether a given user is present on a specified channel (true) or not (false). |
Errors
Whenever the user ID or the channel ID is required, and you try to check the user's presence on a given channel without providing either the user ID or the channel ID, you will receive the User ID is required or the Channel ID is required error.
Sample code
Find out if the support_agent_15 user is present on the support channel.
-
isPresentOn()(on theUserobject)1// reference the user whose presence you want to check
2const user = await chat.getUser("support_agent_15")
3// invoke the "isPresentOn()" method
4const isPresentOn = user.isPresentOn(
5 "support"
6) -
isPresent()(on theChannelobject)1// reference the channel on which you want to check the user's presence
2const channel = await chat.getChannel("support")
3// invoke the "isPresent()" method
4const isPresent = await channel.isPresent(
5 "support_agent_15"
6) -
isPresent()(on theChatobject)1// reference the "chat" object and invoke the "isPresent()" method.
2const isPresent = await chat.isPresent(
3 "support_agent_15",
4 "support"
5)
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 params?: {
3 limit?: number,
4 offset?: number
5 }
6): Promise<string[]> -
whoIsPresent()(on theChatobject)1chat.whoIsPresent(
2 id: string,
3 params?: {
4 limit?: number,
5 offset?: number
6 }
7): Promise<string[]>
Input
| Parameter | Required in the Channel object method | Required in the Chat object method | Description |
|---|---|---|---|
idType: stringDefault: n/a | No | Yes | Unique identifier of the channel where you want to check all present users. |
paramsType: objectDefault: n/a | No | No | Object containing optional pagination parameters. |
→ limitType: numberDefault: 1000 | No | No | Maximum number of occupants to return per channel. Valid range: 0-1000. Use 0 to get occupancy counts without user details. |
→ offsetType: numberDefault: n/a | No | No | Zero-based starting index for pagination. Returns occupants starting from this position in the list. Must be >= 0. |
Output
| Type | Description |
|---|---|
Promise<string[]> | List of all user IDs that are present on the given channel. |
Errors
Whenever the channel ID is required, and you try to check a channel without providing its ID, you will receive the ID is required error.
Sample code
Get a list of users that are present on the support channel.
-
whoIsPresent()(on theChannelobject)1// reference the channel where you want to check all present users
2const channel = await chat.getChannel("support")
3// invoke the "whoIsPresent()" method
4const userIds = await channel.whoIsPresent() -
whoIsPresent()(on theChatobject)1// reference the "chat" object and invoke the "whoIsPresent()" method.
2const userIds = await chat.whoIsPresent(
3 "support"
4)
Get presence updates
Get up-to-date information about the real-time presence of users in the specified channel by subscribing to Presence events. The streamPresence() method 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 signature
This method takes the following parameters:
1channel.streamPresence(
2 callback: (userIds: string[]) => unknown
3): Promise<() => void>
Input
| Parameter | Description |
|---|---|
callback *Type: n/a Default: n/a | Callback function passed as a parameter. It defines the custom behavior to be executed when detecting new user presence events. |
→ userIds *Type: string[]Default: n/a | Returns an updated array of user IDs. |
Output
| Type | Description |
|---|---|
Promise<() => void> | Function you can call to disconnect (unsubscribe) from the channel and stop receiving presence events. |
Sample code
Get user presence updates on support channel.
1const channel = await chat.getChannel("support")
2const stopUpdates = channel.streamPresence((userIds) => {
3 console.log("Currently present users: ", userIds)
4})
5// after some time...
6stopUpdates()
Global presence
The 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 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 through the active method 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
active is a getter method called on the User object that lets you 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 Chat SDK.
Method signature
This method has the following signature:
1user.active: boolean
Properties
| Property | Description |
|---|---|
activeType: boolean | 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), the active method 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// reference the "support_agent_15" user
2const user = await chat.getUser("support_agent_15")
3
4// check if the user has been active in the last 10 minutes
5user.active
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, 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.
Sample code
Show the Unix timestamp when support_agent_15 was last time active in an app.
1// reference the "support_agent_15" user
2const user = await chat.getUser("support_agent_15")
3
4user.lastActiveTimestamp