On this page

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.

icon

Usage in Blueprints and C++


The Chat SDK provides two types of presence tracking:

TypeDescriptionData 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 the User object
  • WherePresent() called on the Chat object.

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

Output
TypeDescription
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 the User object)

    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 the Chat object)

    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 the User object
  • IsPresent() called on the Channel object.
  • IsPresent() called on the Chat object.

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

Output
TypeDescription
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 the User object)

    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 the Channel object)

    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 the Chat object)

    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 the Channel object
  • WhoIsPresent() called on the Chat object.

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

Output
TypeDescription
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 the Channel object)

    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 the Chat object)

    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:

icon

Handle the response


Output
TypeDescription
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 lines

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 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 storeUserActivityTimestamps parameter to true.
  • Deciding how frequently a user's online activity will be updated by configuring the storeUserActivityInterval option - the default value is set to 600000 milliseconds (10 minutes) and the minimum value is 60000 milliseconds (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

Output

TypeDescription
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

Output

TypeDescription
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();
Last updated on