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
- Blueprint
- C++ / Input parameters
-
User->WherePresent()1User->WherePresent() -
Chat->WherePresent()1Chat->WherePresent(FString UserID)
| Parameter | Required in User->WherePresent() | Required in Chat->WherePresent() | Description |
|---|---|---|---|
UserIDType: FStringDefault: n/a | No | Yes | Unique identifier (up to 92 UTF-8 characters) of the user whose presence you want to check. |
Output
| Type | Description |
|---|---|
TArray<FString> | Array of 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#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Define user ID
10FString UserID = "support_agent_15";
11
12// Get the user and save the reference
13UPubnubUser* User = Chat->GetUser(UserID);
14
15TArray<FString> ListOfChannels = User->WherePresent(); -
WherePresent()(on theChatobject)1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Define user ID
10FString UserID = "support_agent_15";
11
12TArray<FString> ListOfChannels = Chat->WherePresent(UserID);
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
- Blueprint
- C++ / Input parameters
-
User->IsPresentOn()1User->IsPresentOn(FString ChannelID); -
Channel->IsPresent()1Channel->IsPresent(FString UserID); -
Chat->IsPresent()1Chat->IsPresent(FString UserID, FString ChannelID);
| Parameter | Required in the User object method | Required in the Channel object method | Required in the Chat object method | Description |
|---|---|---|---|---|
UserIDType: FStringDefault: n/a | No | Yes | Yes | Unique ID (up to 92 UTF-8 characters) of the user whose presence you want to check. |
ChannelIDType: FStringDefault: n/a | Yes | No | Yes | Unique identifier of the channel where you want to check the user's presence. |
Output
| Type | Description |
|---|---|
bool | Returns 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#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Get the user and save the reference
10UPubnubUser* User = Chat->GetUser("support_agent_15");
11
12bool IsPresent = User->IsPresentOn("support"); -
IsPresent()(on theChannelobject)1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Get the channel and save the reference
10UPubnubChannel* Channel = Chat->GetChannel("support");
11
12bool IsPresent = Channel->IsPresent("support_agent_15"); -
IsPresent()(on theChatobject)1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9bool IsPresent = Chat->IsPresent("support_agent_15", "support");
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
- Blueprint
- C++ / Input parameters
-
Channel->WhoIsPresent()1Channel->WhoIsPresent(); -
Chat->WhoIsPresent()1Chat->WhoIsPresent(FString ChannelID);
| Parameter | Required in the Channel object method | Required in the Chat object method | Description |
|---|---|---|---|
ChannelIDType: FStringDefault: n/a | No | Yes | Unique identifier of the channel where you want to check the user's presence. |
Output
| Type | Description |
|---|---|
TArray<FString> | Array of all user IDs that are present on the given channel. |
Sample code
Get a list of users that are present on the support channel.
-
WhoIsPresent()(on theChannelobject)1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9TArray<FString> UserIDs = Channel->WhoIsPresent(); -
WhoIsPresent()(on theChatobject)1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9TArray<FString> UserIDs = Chat->WhoIsPresent("support");
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:
- Blueprint
- C++ / Input parameters
1Channel->StreamPresence(FOnPubnubChannelStreamPresenceReceived PresenceCallback);
| Parameter | Description |
|---|---|
PresenceCallbackType: FOnPubnubChannelStreamPresenceReceivedDefault: n/a | Callback function passed as a parameter. It defines the custom behavior to be executed when detecting new user presence events. |
Output
| Type | Description |
|---|---|
UPubnubCallbackStop* | Object on which you can call Stop() to stop receiving updates. |
Sample code
Get user presence updates on support channel.
1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Get the channel and save the reference
10UPubnubChannel* Channel = Chat->GetChannel("support");
11
12// Create a pubnub response delegate
13// you MUST implement your own callback function to handle the response
14FOnPubnubChannelStreamPresenceReceived PresenceResponse;
15PresenceResponse.BindDynamic(this, &AMyActor::OnPresenceResponseReceived);
show all 17 linesGlobal 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 Unreal 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 Unreal 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 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 Unreal Chat SDK.
Method signature
- Blueprint
- C++ / Input parameters
1User->Active();
Output
| Type | Description |
|---|---|
bool | Whether the user is active or not. |
Sample code
Return the current chat user's data.
1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Define user ID
10FString UserID = "support_agent_15";
11
12// Get the user and save the reference
13UPubnubUser* User = Chat->CurrentUser();
14
15bool IsUserActive = User->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.
Method signature
- Blueprint
- C++ / Input parameters
1User->LastActiveTimeStamp();
Output
| Type | Description |
|---|---|
FString | A timestamp for the last time the user was active. |
Sample code
Return the current chat user's data.
1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9// Define user ID
10FString UserID = "support_agent_15";
11
12// Get the user and save the reference
13UPubnubUser* User = Chat->CurrentUser();
14
15FString WhenUserWasLastActive = User->LastActiveTimestamp();