Presence

In your chat app, you might want to graphically show up-to-date information on all users present in an app or on a given channel, showing if they are online, offline, active, or away. You may also want to let other chat users know when was the last time a given user was online.

icon

Usage in Blueprints and C++


Unreal Chat SDK provides methods and options to determine user presence on the channel and the app (global) level. Additionally, it provides a way of checking the last time the user performed some online activity in the app based on a Unix timestamp.

To find out the channel presence of a user, Unreal Chat SDK uses the PubNub Presence API. It provides a set of convenience methods for checking which channels a given user is subscribed to. Importantly, you can stream presence events and always get real-time information about the user's channel presence. You can also set dynamic custom state for users on one or more channels. This custom state persists on a channel as long as the user stays subscribed. Some examples of custom states are to add your score, game state, or location in an application if it changes frequently.

For global (app) presence, however, Unreal Chat SDK couldn't rely on Presence API as it doesn't support global user presence, but is limited to the context of a given channel. To fill that gap, the Unreal Chat SDK offers an optional lastActiveTimestamp property on each User object that states when was the last time the user was active in an app.

Suppose you want to track users' global presence in your chat app (for example, to see a user's current online state when browsing the users). In that case, you can configure your app to monitor this property and set an interval to update it. You can later use a dedicated active method to retrieve that information and display it across the app or in the context of a given channel, if you like. Contrary to the channel presence, global user presence info is not real-time information and depends on how often you update it - the default value is 600000 milliseconds (10 minutes) and the minimum value is 60000 milliseconds (1 minute). You can set a preferred value during the Unreal Chat SDK initialization through the storeUserActivityTimestamps parameter.

Read the sections to learn how to use all Unreal Chat SDK options and what configuration is required to implement channel and global presence in your app.

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.

Basic usage

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

  • WherePresent() (on the User object)

    #include "Kismet/GameplayStatics.h"
    #include "PubnubChatSubsystem.h"

    UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

    UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

    // Define user ID
    FString UserID = "support_agent_15";

    // Get the user and save the reference
    UPubnubUser* User = Chat->GetUser(UserID);

    TArray<FString> ListOfChannels = User->WherePresent();
  • WherePresent() (on the Chat object)

    #include "Kismet/GameplayStatics.h"
    #include "PubnubChatSubsystem.h"

    UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

    UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

    // Define user ID
    FString UserID = "support_agent_15";

    TArray<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
boolReturns information on whether a given user is present on a specified channel (true) or not (false).

Basic usage

Find out if the support_agent_15 user is present on the support channel.

  • IsPresentOn() (on the User object)

    #include "Kismet/GameplayStatics.h"
    #include "PubnubChatSubsystem.h"

    UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

    UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

    // Get the user and save the reference
    UPubnubUser* User = Chat->GetUser("support_agent_15");

    bool IsPresent = User->IsPresentOn("support");
  • IsPresent() (on the Channel object)

    #include "Kismet/GameplayStatics.h"
    #include "PubnubChatSubsystem.h"

    UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

    UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

    // Get the channel and save the reference
    UPubnubChannel* Channel = Chat->GetChannel("support");

    bool IsPresent = Channel->IsPresent("support_agent_15");
  • IsPresent() (on the Chat object)

    #include "Kismet/GameplayStatics.h"
    #include "PubnubChatSubsystem.h"

    UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

    UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

    bool 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.

Basic usage

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

  • WhoIsPresent() (on the Channel object)

    #include "Kismet/GameplayStatics.h"
    #include "PubnubChatSubsystem.h"

    UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

    UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

    TArray<FString> UserIDs = Channel->WhoIsPresent();
  • WhoIsPresent() (on the Chat object)

    #include "Kismet/GameplayStatics.h"
    #include "PubnubChatSubsystem.h"

    UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
    UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

    UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

    TArray<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 PubNub 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.

Basic usage

Get user presence updates on support channel.

#include "Kismet/GameplayStatics.h"
#include "PubnubChatSubsystem.h"

UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();

UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");

// Get the channel and save the reference
UPubnubChannel* Channel = Chat->GetChannel("support");

// Create a pubnub response delegate
// you MUST implement your own callback function to handle the response
FOnPubnubChannelStreamPresenceReceived PresenceResponse;
PresenceResponse.BindDynamic(this, &AMyActor::OnPresenceResponseReceived);
show all 17 lines
Last updated on