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
Return a list of channels where a given user is present:
wherePresent()on theUserobjectwherePresent()on theChatobject
Method signature
These methods take the following parameters:
-
wherePresent()(on theUserobject)1user.wherePresent() async throws -> [String] -
wherePresent()(on theChatobject)1chat.wherePresent(
2 userId: String
3) async throws -> [String]
Input
userId#- Unique identifier (up to 92 UTF-8 characters) of the user whose presence you want to check.
- Required in the User object method
- No
- Required in the Chat object method
- Yes
Output
[String]#- List of all channel IDs on which the given user is present.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
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
Check if a user is present on a specified channel:
isPresentOn()on theUserobjectisPresent()on theChannelobjectisPresent()on theChatobject
Method signature
These methods take the following parameters:
-
isPresentOn()(on theUserobject)1user.isPresentOn(
2 channelId: String
3) async throws -> Bool -
isPresent()(on theChannelobject)1channel.isPresent(
2 userId: String
3) async throws -> Bool -
isPresent()(on theChatobject)1chat.isPresent(
2 userId: String,
3 channelId: String
4) async throws -> Bool
Input
userId#- Unique ID (up to 92 UTF-8 characters) of the user whose presence you want to check.
- Required in the User object method
- No
- Required in the Channel object method
- Yes
- Required in the Chat object method
- Yes
channelId#- Unique identifier of the channel where you want to check the user's presence.
- Required in the User object method
- Yes
- Required in the Channel object method
- No
- Required in the Chat object method
- Yes
Output
Bool#- Returns information on whether a given user is present on a specified channel (
true) or not (false) or an error.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Find out if the support_agent_15 user is present on the support channel.
-
isPresentOn()(on theUserobject)1 -
isPresent()(on theChannelobject)1 -
isPresent()(on theChatobject)1
Return all users present on channel
Return a list of users present on a given channel:
whoIsPresent()on theChannelobjectwhoIsPresent()on theChatobject
Method signature
These methods take the following parameters:
-
whoIsPresent()(on theChannelobject)1channel.whoIsPresent(
2 limit: Int = 1000,
3 offset: Int? = 0
4) async throws -> [String] -
whoIsPresent()(on theChatobject)1chat.whoIsPresent(
2 channelId: String,
3 limit: Int = 1000,
4 offset: Int? = 0
5) async throws -> [String]
Input
channelId#- Unique identifier of the channel where you want to check all present users.
- Required in the Channel object method
- No
- Required in the Chat object method
- Yes
limit#- Maximum number of occupants to return per channel. Valid range: 0-1000. Use
0to get occupancy counts without user details.- Required in the Channel object method
- No
- Required in the Chat object method
- No
offset#- Zero-based starting index for pagination. Returns occupants starting from this position in the list. Must be >= 0.
- Required in the Channel object method
- No
- Required in the Chat object method
- No
Output
[String]#- List of all user IDs that are present on the given channel.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Get a list of users that are present on the support channel.
-
whoIsPresent()(on theChannelobject)1 -
whoIsPresent()(on theChatobject)1
Get presence updates
Get real-time presence updates by subscribing to Presence events. Use onPresenceChanged() to track who connects to or disconnects from the channel via a closure, or channel.stream.presenceChanges() for an AsyncStream-based approach.
Deprecation
streamPresence() is deprecated. Use onPresenceChanged() (closure-based) or channel.stream.presenceChanges() (AsyncStream-based) instead.
Method signature
1channel.onPresenceChanged(
2 callback: @escaping (Set<String>) -> Void
3) -> AutoCloseable
Input
callbackrequired#- Closure called with the set of user IDs currently present on the channel whenever the presence state changes.
Output
AutoCloseable#- An object you must retain. When released or closed, the listener stops.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Get user presence updates on support channel.
1
Presence update delay
Closing the AutoCloseable cancels the local subscription. It does not push an immediate update to other subscribers. The delay other clients experience before seeing the presence change depends on network propagation and is outside the SDK's control.
Once channel occupancy exceeds the Announce Max threshold, individual join, leave, and timeout events are replaced by periodic interval events, whether or not Presence Deltas are enabled. In that case, other subscribers see the update at the next scheduled interval. To reduce this window, lower the Interval value in your keyset's Presence settings in Admin Portal.
Global presence
Track users' last online activity to display availability status (offline, online, active, away) next to user profiles.
This feature relies on the lastActiveTimestamp property on the User object. To enable tracking, configure during Chat SDK initialization:
- Set
storeUserActivityTimestampstotrue - Configure
storeUserActivityIntervalfor update frequency (default: 600 seconds/10 minutes, minimum: 60 seconds/1 minute)
Use the active property to check if a user showed activity within the configured interval.
Check user's app presence
active is a getter property on the User object that checks whether a user has recently been active based on their last activity timestamp and the 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
Input
This method doesn't take any parameters.
Output
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 thestoreUserActivityIntervalparameter to the default600seconds (10 minutes) and the user has been active in the app within the last 10 minutes (based on theirlastActiveTimestampproperty), theactivemethod returnstrue.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
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.
Retrieve the lastActiveTimestamp from the User object and convert it to a human-readable date to display when a user was last online.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Show the Unix timestamp when support_agent_15 was last time active in an app.
1