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

# Handling a social relationship change

> XSAPI code for subscribing to XBOX Live social relationship change events and adding or removing handlers when a user's friends list is updated.

This topic provides example code for handling a social relationship change.

## Subscribing to a relationship change

**Flat C API**

```cpp theme={null}
HRESULT hr = XblSocialSubscribeToSocialRelationshipChange(
    xboxLiveContext, 
    xboxUserId,
    &state.socialSubscriptionHandle
);
```

## Unsubscribing from a relationship change

**Flat C API**

```cpp theme={null}
HRESULT hr = XblSocialUnsubscribeFromSocialRelationshipChange(
    xboxLiveContext, 
    state.socialSubscriptionHandle
);

state.socialSubscriptionHandle = nullptr;
```

## Adding a relationship-changed handler

**Flat C API**

```cpp theme={null}
state.socialRelationshipChangedHandlerToken = XblSocialAddSocialRelationshipChangedHandler(
    xboxLiveContext,
    [](const XblSocialRelationshipChangeEventArgs* args, void* context)
    {
        UNREFERENCED_PARAMETER(context);
        LogToFile("Social relationship changed:");
        std::stringstream ss;
        for (size_t i = 0; i < args->xboxUserIdsCount; ++i)
        {
            if (i > 0) 
            {
                ss << ", ";
            }
            ss << args->xboxUserIds[i];
        }
        LogToFile("socialNotification = %u, affectedXuids = %s", args->socialNotification, ss.str().data());
    },
    nullptr
);
```

## Removing a relationship-changed handler

**Flat C API**

```cpp theme={null}
HRESULT hr = XblSocialRemoveSocialRelationshipChangedHandler(xboxLiveContext, state.socialRelationshipChangedHandlerToken);
state.socialRelationshipChangedHandlerToken = 0;
```

## See also

[XblSocialAddSocialRelationshipChangedHandler](/reference/live/xsapi-c/social_c/functions/xblsocialaddsocialrelationshipchangedhandler)
[XblSocialRemoveSocialRelationshipChangedHandler](/reference/live/xsapi-c/social_c/functions/xblsocialremovesocialrelationshipchangedhandler)


## Related topics

- [People System (Friends List) example code](/services/xbox-services/community/people-system/how-to/live-pplsys-howto-nav.md)
- [How to](/services/xbox-services/community/people-system/how-to/index.md)
- [XblSocialRelationshipChangeEventArgs](/reference/live/xsapi-c/social_c/structs/xblsocialrelationshipchangeeventargs.md)
- [XblSocialAddSocialRelationshipChangedHandler](/reference/live/xsapi-c/social_c/functions/xblsocialaddsocialrelationshipchangedhandler.md)
- [XblSocialRemoveSocialRelationshipChangedHandler](/reference/live/xsapi-c/social_c/functions/xblsocialremovesocialrelationshipchangedhandler.md)
