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

# XGameStreamingRegisterConnectionStateChanged

> XGameStreamingRegisterConnectionStateChanged

# XGameStreamingRegisterConnectionStateChanged

Registers a callback to be invoked when a streaming client device's connection state changes.  Upon registration, the callback will be invoked for all streaming client devices that are already connected.

## Syntax

```cpp theme={null}
HRESULT XGameStreamingRegisterConnectionStateChanged(  
         XTaskQueueHandle queue,  
         void* context,  
         XGameStreamingConnectionStateChangedCallback* callback,  
         XTaskQueueRegistrationToken* token  
)  
```

### Parameters

*queue*   \_In\_opt\_\
Type: XTaskQueueHandle

Handle to the asynchronous queue to put the change callback on.

*context*   \_In\_opt\_\
Type: void\*

An optional pointer to the context passed to the callback function.

*callback*   \_In\_\
Type: XGameStreamingConnectionStateChangedCallback\*

The callback function that will be invoked when any streaming client device's connection state changes.

*token*   \_Out\_\
Type: XTaskQueueRegistrationToken\*

A pointer to the registration token which can be used to unregister from the event.

### Return value

Type: HRESULT

Returns **S\_OK** if successful; otherwise, returns an error code.

#### Potential Errors

| Error Code                         | Error Value | Reason for Error                                                                                                                                                                     |
| ---------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| E\_GAMESTREAMING\_NOT\_INITIALIZED | 0x89245400  | The XGameStreaming runtime has not been initialized. Call [XGameStreamingInitialize](/reference/system/xgamestreaming/functions/xgamestreaminginitialize) before calling other APIs. |

For a list of error codes, see [Error Codes](/reference/errorcodes).

## Remarks

<Note>This function isn't safe to call on a time-sensitive thread. For more information, see [Time-sensitive threads](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads).</Note>

This API is the recommended way to have the game do customization per streaming client device and maintain state that allows the game to optimize the experience based on whether there are any devices connected.

<Note>Upon registration, the callback will be invoked for all streaming client devices that are currently connected.</Note>

## Example

```cpp theme={null}
    // Called during game startup to hook into any game streaming start/stop events
    void SetupStreamAware()
    {
        // Register a callback for client connections and disconnections. Note that the connection callback
        // will fire for any already connected clients, so do all of our per-client setup in that callback.      
        XGameStreamingConnectionStateChangedRegistrationToken stateChangeToken = {0};
        XGameStreamingRegisterConnectionStateChanged(
            m_taskQueue, this, ConnectionStateChangedCallback, &stateChangeToken);
    }

    static void ConnectionStateChangedCallback(void* context, XGameStreamingClientId client, XGameStreamingConnectionState state)
    {
        static_cast<Game*>(context)->OnConnectionStateChanged(client, state);
    }

    void OnConnectionStateChanged(XGameStreamingClientId client, XGameStreamingConnectionState state)
    {
        switch (state)
        {
        case XGameStreamingConnectionState::Connected: 
            OnClientConnected(client); 
            break;
        case XGameStreamingConnectionState::Disconnected: 
            OnClientDisconnected(client); 
            break;
        default: 
            break;
        }
    }


    void OnClientConnected(XGameStreamingClientId client)
    {
        // Update list of connected clients
        m_streamingClients.push_back(client);

        // Do per-client initialization
        // ...
    }

    void OnClientDisconnected(XGameStreamingClientId client)
    {
        // Release our reference on the client that is no longer streaming the game
        std::remove(m_streamingClients.begin(), m_streamingClients.end(), client);

        // Do per-client cleanup
        // ...
    }



```

## Requirements

**Header:** xgamestreaming.h

**Library:** xgameruntime.lib

**Supported platforms:** Windows, XBOX One family consoles and XBOX Series consoles

## Conceptual documentation

* [Cloud Aware API calls](/build/core-features/common/game-streaming/game-streaming-am-i-streaming)
* [Time-sensitive threads](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## See also

[XGameStreamingConnectionState](/reference/system/xgamestreaming/enums/xgamestreamingconnectionstate)\
[XGameStreamingConnectionStateChangedCallback](/reference/system/xgamestreaming/functions/xgamestreamingconnectionstatechangedcallback)\
[XGameStreamingUnregisterConnectionStateChanged](/reference/system/xgamestreaming/functions/xgamestreamingunregisterconnectionstatechanged)\
[XGameStreaming](/reference/system/xgamestreaming/xgamestreaming_members)


## Related topics

- [XGameStreamingUnregisterConnectionStateChanged](/reference/system/xgamestreaming/functions/xgamestreamingunregisterconnectionstatechanged.md)
- [XGameStreamingConnectionStateChangedCallback](/reference/system/xgamestreaming/functions/xgamestreamingconnectionstatechangedcallback.md)
- [XGameStreamingConnectionState](/reference/system/xgamestreaming/enums/xgamestreamingconnectionstate.md)
- [XGameStreamingIsStreaming](/reference/system/xgamestreaming/functions/xgamestreamingisstreaming.md)
- [XGameStreamingGetConnectionState](/reference/system/xgamestreaming/functions/xgamestreaminggetconnectionstate.md)
