Programming the RTA service

This topic demonstrates how to call the Real-Time Activity (RTA) service by using the following flat C code examples.

Registering a handler to get statistic change events from the RTA service

You define your statistics and configure them for RTA in Partner Center. For more information, see the following:

Note

If you're an event-based statistics developer, contact your developer account manager (DAM) for information about portal configuration of event-based statistics in Partner Center. For more information, see Configure Xbox Live stats and stat rules in Partner Center).

Within your title, you need to configure the statistics that you want RTA to track. Title handlers are invoked whenever one of the configured statistics changes as shown in the following code example.

Flat C

// Add a statistic changed handler.
void* context{ nullptr };
XblFunctionContext statisticChangedFunctionContext = XblUserStatisticsAddStatisticChangedHandler(
    xboxLiveContext,
    [](XblStatisticChangeEventArgs eventArgs, void* context)
    {
        // Handle statistic change. 
        LogToScreen("Statistic changed callback: stat changed (%s = %s)",
            eventArgs.latestStatistic.statisticName,
            eventArgs.latestStatistic.value);
    },
    context
    );

// Configure the statistics that you want RTA to track. Titles only receive real-time updates for tracked statistics.
// Note that you can update the set of tracked statistics independently from the handlers.
std::vector<const char*> statisticNames{ "TotalPuzzlesSolved" };
HRESULT hr = XblUserStatisticsTrackStatistics(
    xblContextHandle,
    &xboxUserId,
    1,
    scid,
    statisticNames.data(),
    statisticNames.size()
);

Unregistering from the RTA service

When updates are no longer needed for a particular statistic (or a set of statistics), your title should simply stop tracking that statistic. If statistics updates are no longer needed altogether, removing all registered handlers automatically removes the related RTA subscriptions as shown in the following code example.

Flat C

// Stop receiving updates for a particular statistic.   
std::vector<const char*> statisticNames{ "TotalPuzzlesSolved" }; 
HRESULT hr = XblUserStatisticsStopTrackingStatistics(
    xblContextHandle,
    &xboxUserId,
    1,
    scid,
    statisticNames.data(),
    statisticNames.size()
);

// Alternatively, stop receiving updates for statistics changes altogether.
XblUserStatisticsRemoveStatisticChangedHandler(
    xblContextHandle,
    statisticChangedFunctionContext
);

Important

If a client uses RTA for multiplayer sessions, and is disconnected for thirty seconds, the Multiplayer Session Directory (MPSD) detects that the RTA session is closed, and removes the player from the session. XSAPI then automatically reestablishes the RTA connection. However, it's the title's responsibility to rewrite its MPSD sessions after the RTA subscription has been reestablished.

See also

XblStatisticChangeEventArgs

XblUserStatisticsAddStatisticChangedHandler

XblUserStatisticsRemoveStatisticChangedHandler