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++


Asynchronous and synchronous method execution

Most PubNub Unreal SDK methods are available in both asynchronous and synchronous variants.

  • Asynchronous methods (Async suffix) return void and take an optional delegate parameter that fires when the operation completes.

    1User->WherePresentAsync(OnWherePresentResponseDelegate);

    You can also use native callbacks that accept lambdas instead of dynamic delegates. Native callback types have the Native suffix (for example, FOnPubnubChatWherePresentResponseNative).

  • Synchronous methods (no suffix) block the main game thread until the operation completes and return a result struct directly.

    1FPubnubChatWherePresentResult Result = User->WherePresent();

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. Presence events are delivered automatically via the channel's OnPresenceChanged multicast delegate when the channel is connected.

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

  • User->WherePresent()

    1User->WherePresent();
  • Chat->WherePresent()

    1Chat->WherePresent(FString UserID);
ParameterRequired in User->WherePresent()Required in Chat->WherePresent()Description
UserID
Type: FString
Default:
n/a
No
Yes
Unique identifier (up to 92 UTF-8 characters) of the user whose presence you want to check.
Output
MethodReturn typeDescription
User->WherePresent()
FPubnubChatWherePresentResult
Contains Result (operation result) and Channels (TArray<FString>).
Chat->WherePresent()
FPubnubChatWherePresentResult
Contains Result (operation result) and Channels (TArray<FString>).

Sample code

Reference code

This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.

Get a list of channels on which a user is present.

WherePresent() (on the User object)

Actor.h
1

Actor.cpp
1

WherePresent() (on the Chat object)

Actor.h
1

Actor.cpp
1

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

  • User->IsPresentOn()

    1User->IsPresentOn(FString ChannelID);
  • Channel->IsPresent()

    1Channel->IsPresent(FString UserID);

    Returns FPubnubChatIsPresentResult (contains Result with Error/ErrorMessage and IsPresent bool).

  • Chat->IsPresent()

    1Chat->IsPresent(FString UserID, FString ChannelID);
ParameterRequired in the User object methodRequired in the Channel object methodRequired in the Chat object methodDescription
UserID
Type: FString
Default:
n/a
No
Yes
Yes
Unique ID (up to 92 UTF-8 characters) of the user whose presence you want to check.
ChannelID
Type: FString
Default:
n/a
Yes
No
Yes
Unique identifier of the channel where you want to check the user's presence.
Output
MethodReturn type
User->IsPresentOn()
FPubnubChatIsPresentResult (contains Result with Error/ErrorMessage and IsPresent bool)
Channel->IsPresent()
FPubnubChatIsPresentResult (contains Result with Error/ErrorMessage and IsPresent bool)
Chat->IsPresent()
FPubnubChatIsPresentResult (contains Result with Error/ErrorMessage and IsPresent bool)

Sample code

Reference code

This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.

Find out if a user is present on a channel.

IsPresentOn() (on the User object)

Actor.h
1

Actor.cpp
1

IsPresent() (on the Channel object)

Actor.h
1

Actor.cpp
1

IsPresent() (on the Chat object)

Actor.h
1

Actor.cpp
1

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

  • Channel->WhoIsPresent()

    1Channel->WhoIsPresent(int Limit = 1000, int Offset = 0);
  • Chat->WhoIsPresent()

    1Chat->WhoIsPresent(FString ChannelID, int Limit = 1000, int Offset = 0);
ParameterRequired in the Channel object methodRequired in the Chat object methodDescription
ChannelID
Type: FString
Default:
n/a
No
Yes
Unique identifier of the channel where you want to check the user's presence.
Limit
Type: int
Default:
1000
No
No
Maximum number of user IDs to return.
Offset
Type: int
Default:
0
No
No
Number of user IDs to skip for pagination.
Output
MethodReturn type
Channel->WhoIsPresent()
FPubnubChatWhoIsPresentResult (contains Result and Users TArray<FString>)
Chat->WhoIsPresent()
FPubnubChatWhoIsPresentResult (contains Result and Users TArray<FString>)

Sample code

Reference code

This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.

Get a list of users that are present on a channel.

WhoIsPresent() (on the Channel object)

Actor.h
1

Actor.cpp
1

WhoIsPresent() (on the Chat object)

Actor.h
1

Actor.cpp
1

Get presence updates

Call StreamPresence() on a Channel object to start receiving real-time presence events (who joins or leaves the channel). Presence updates are delivered through the channel's OnPresenceChanged multicast delegate, which broadcasts a TArray<FString> of user IDs currently present on the channel. Call StopStreamingPresence() to stop receiving updates.

You can use this information to visually represent user availability in your chat app through statuses like offline, online, active, away, or any other.

Method signature

  • Channel->StreamPresence()

    1Channel->StreamPresence();
  • Channel->StopStreamingPresence()

    1Channel->StopStreamingPresence();
Output
MethodReturn typeDescription
Channel->StreamPresence()
FPubnubChatOperationResult
Contains Error (bool) and ErrorMessage (FString). Success if the presence listener was started.
Channel->StopStreamingPresence()
FPubnubChatOperationResult
Contains Error (bool) and ErrorMessage (FString). Success if the presence listener was stopped.
Delegate
DelegateParameterDescription
OnPresenceChanged
const TArray<FString>& UserIDs
Broadcasts the list of user IDs currently present on the channel whenever the presence set changes.

Sample code

Start streaming presence on the support channel, bind the delegate to log user presence changes, and stop streaming when done.

1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7FPubnubChatInitChatResult InitResult = PubnubChatSubsystem->InitChat("demo", "demo", "my_user");
8UPubnubChat* Chat = InitResult.Chat;
9
10FPubnubChatChannelResult ChannelResult = Chat->GetChannel("support");
11UPubnubChatChannel* Channel = ChannelResult.Channel;
12
13// Bind the delegate to receive presence updates
14Channel->OnPresenceChanged.AddDynamic(this, &AMyActor::OnPresenceUpdated);
15
show all 20 lines

Other examples

Stream presence on a channel

Actor.h
1

Actor.cpp
1

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 IsActive() 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

IsActive() 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

1User->IsActive();

Output

TypeDescription
bool
Whether the user is active or not.

Sample code

Reference code

This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.

Return the current chat user's data.

Actor.h
1

Actor.cpp
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, 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

1User->GetLastActiveTimestamp();

Output

TypeDescription
FString
A timestamp for the last time the user was active.

Sample code

Reference code

This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.

Return the current chat user's data.

Actor.h
1

Actor.cpp
1

Last updated on