Skip to main content

Integrating presence with the client SDK

Last updated on January 31, 2024

Overview

The Presence service can be used to set and retrieve player presence status so you can keep your user base informed of what other players are currently doing in game. To manage these statuses, you will use the Lobby interface to call presence functions, which then will be sent through WebSocket to ensure statuses are updated in real-time.

Because each game is unique and may need to provide more than just online / offline states, we provide two parameters that you can set and retrieve:

  • Availability - Can be set to one of the following states: Offline, Available, Busy, Away, and Invisible. If a player disconnects from the Lobby, this will automatically be updated to offline.

  • Activity - This is customizable in order to allow the game to provide unique / specific details about a player’s current state such as Playing a Game, In Lobby, In a Match, or In Party Looking For Members.

Implementing Presence

When integrating the service into your game client, keep in mind this general flow for setting and retrieving player statuses:

  1. On game start, connect to the Lobby service and set the player’s status to Available.
  2. Retrieve the statuses of a player’s friends list so they can be displayed.
  3. Register to listen for changes to friend statuses so that they can be updated.
  4. As the player changes game play activities, be sure to set their status accordingly.

Set Player Status

To set a player’s presence from the game client, you will call the update function and provide both the availability and activity states you want to set for the player as parameters.

Availability Availability = Availability::Available;
FString Activity = FString("My New Activity");

FRegistry::Lobby.Connect();
FRegistry::Lobby.SendSetPresenceStatus(Availability, Activity);

Listen for changes to Friend Statuses

In order to receive real time updates about a friend's status, you will need to register to listen for notifications. As a friend's status changes you can inform the player through the game client of the change, such as letting the player know a friend just came online.

FRegistry::Lobby.Connect();
FRegistry::Lobby.SetUserPresenceNotifDelegate(AccelByte::Api::Lobby::FFriendStatusNotif::CreateLambda([](const FAccelByteModelsUsersPresenceNotice& Result)
{
// Do something if UserPresenceNotifDelegate has been successful
}));

Retrieve the Friends List Statuses

To provide presence statuses as part of displaying a friends list to players, you can call this function to get the status for all the player’s friends.

FRegistry::Lobby.Connect();
FRegistry::Lobby.SetGetOnlineFriendsPresenceResponseDelegate(AccelByte::Api::Lobby::FGetAllFriendsStatusResponse::CreateLambda([](const FAccelByteModelsGetOnlineUsersResponse& Result)
{
if (Result.Code == "0")
{
// Do something if GetOnlineFriendsPresenceResponseDelegate has been successful
}
else
{
// Do something if GetOnlineFriendsPresenceResponseDelegate has an error
}
}));

FRegistry::Lobby.SendGetOnlineFriendPresenceRequest();

Retrieve Player Statuses in Bulk

In cases where you need to provide presence status between player’s who are not friends, you can use this function by passing in a list of User IDs. This is useful in situations such as players who are in a group or raid team who need to coordinate with each other even without being friends. This function can also be used to provide the count of players in a given status by setting the countOnly parameter to true when calling it.

TArray<FString> UserIds = {FString("12345abcd"), FString("abcd12345")};
bool CountOnly = true;

FRegistry::Lobby.Connect();
FRegistry::Lobby.BulkGetUserPresence(UserIds, THandler<FAccelByteModelsBulkUserStatusNotif>::CreateLambda([](const FAccelByteModelsBulkUserStatusNotif& Result)
{
// Do something if BulkGetUserPresence has been successful
}), FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something if BulkGetUserPresence has an error
UE_LOG(LogTemp, Log, TEXT("Error BulkGetUserPresence, Error Code: %d Error Message: %s"), ErrorCode, *ErrorMessage);
}), CountOnly);