> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Programming the RTA service

> Flat C code samples showing how to register and unregister XSAPI handlers with the XBOX Real-Time Activity (RTA) service to receive player stat changes.

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](#registering-a-handler-to-get-statistic-changes-events-from-the-Real-Time-Activity-service)
* [Unregistering from the RTA service](#unregistering-from-the-Real-Time-Activity-service)

<a id="registering-a-handler-to-get-statistic-changes-events-from-the-Real-Time-Activity-service" />

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

You define your statistics and configure them for RTA in [Partner Center](https://partner.microsoft.com/dashboard). For more information, see the following:

* [Configuring title-managed featured stats and leaderboards in Partner Center](/services/xbox-services/player-data/stats-leaderboards/title-managed/config/live-tm-leaderboards-portal)

* [Registering for player stat change notifications](/services/xbox-services/fundamentals/rta/concepts/live-register-for-stat-notifications)

<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](https://partner.microsoft.com/dashboard). For more information, see [Configure XBOX Live stats and stat rules in Partner Center)](https://developer.microsoft.com/en-us/games/xbox/partner/windows-configure-stats).</Note>

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

```cpp theme={null}
// 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()
);
```

<a id="unregistering-from-the-Real-Time-Activity-service" />

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

```cpp theme={null}
// 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
);
```

<Info>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.</Info>

## See also

[XblStatisticChangeEventArgs](/reference/live/xsapi-c/user_statistics_c/structs/xblstatisticchangeeventargs)

[XblUserStatisticsAddStatisticChangedHandler](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsaddstatisticchangedhandler)

[XblUserStatisticsRemoveStatisticChangedHandler](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsremovestatisticchangedhandler)


## Related topics

- [Real-Time Activity (RTA) service example code](/services/xbox-services/fundamentals/rta/how-to/live-rta-howto-nav.md)
- [How to](/services/xbox-services/fundamentals/rta/how-to/index.md)
- [Portal configuration of stat change notifications](/services/xbox-services/player-data/stats-leaderboards/event-based/config/live-stat-change-notif-portal.md)
- [Best practices for the RTA service](/services/xbox-services/fundamentals/rta/concepts/live-rta-best-practices.md)
- [Real-Time Activity (RTA) service](/services/xbox-services/fundamentals/rta/live-rta-nav.md)
