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

# Real-time messages SignalR Hub

> Connect PlayFab Lobby and Matchmaking clients to the real-time messages SignalR Hub, including HubConnection setup, MsgPack encoding, and KeepAlive intervals.

<Note>
  The REST and SignalR APIs documented here are more complex than the client SDKs. Consider using the [Lobby C++ SDK](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/pflobby_members) or [Matchmaking C++ SDK](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pfmatchmaking/pfmatchmaking_members) instead, unless those SDKs don't meet your needs.
</Note>

The real-time messages feature for [Lobby and Matchmaking
services](/services/playfab/multiplayer/lobby/lobby-and-matchmaking) works through a
[SignalR service](https://learn.microsoft.com/en-us/aspnet/core/signalr), which exposes
a [SignalR Hub](https://learn.microsoft.com/en-us/aspnet/core/signalr/introduction#hubs). This
SignalR Hub can be used by game clients to receive messages about [resources
they subscribed to](/services/playfab/multiplayer/real-time-messages/subscribing-to-resources).

## Connecting to the SignalR Hub

The first step in receiving real-time messages is to create a [SignalR
client](https://learn.microsoft.com/en-us/aspnet/core/signalr/client-features). The
client can create a connection to the SignalR Hub by [instantiating a
HubConnection](https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client#connect-to-a-hub)
with the following API: [SignalR Negotiate REST
API](https://learn.microsoft.com/en-us/rest/api/playfab/multiplayer/pub-sub/negotiate).
This API implements the Negotiate API described [in the SignalR
documentation](https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/TransportProtocols.md#post-endpoint-basenegotiate-request).

<Warning>
  Using [MessagePack (MsgPack) encoding](https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/HubProtocol.md#messagepack-msgpack-encoding) when connecting to the SignalR Hub is strongly recommended, and will be enforced as the only supported encoding in the future.
</Warning>

<Info>
  The client **must** set `KeepAliveInterval` to **5 seconds** when building the `HubConnection`. The backend service will time out and close connections that do not receive a keep-alive ping within **10 seconds**. The default SignalR `KeepAliveInterval` of 15 seconds is too slow and will cause disconnections. `csharp var connection = new HubConnectionBuilder() .WithUrl(hubUrl, options => { /* auth headers */ }) .WithKeepAliveInterval(TimeSpan.FromSeconds(5)) .Build(); ` For more details on SignalR client configuration, see the [SignalR configuration documentation](https://learn.microsoft.com/en-us/aspnet/core/signalr/configuration#configure-client-options).
</Info>

<Note>
  1. SignalR client implementations expect the URL to omit the `/negotiate` portion of this path (for example, HubConnection should be created to a URL that ends in `...playfabapi.com/pubsub`). 1. The auth headers required to call this API can be passed to the SignalR client via `HttpConnectionOptions` as [seen on this method](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.signalr.client.hubconnectionbuilderhttpextensions.withurl#microsoft-aspnetcore-signalr-client-hubconnectionbuilderhttpextensions-withurl\(microsoft-aspnetcore-signalr-client-ihubconnectionbuilder-system-string-system-action\(\(microsoft-aspnetcore-http-connections-client-httpconnectionoptions\)\)\)).
</Note>

## Implementing client methods to receive messages

See the SignalR Client [documentation about implementing client
methods](https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client#call-client-methods-from-hub)
that will be invoked to receive messages. Clients should implement all of the
methods listed in [Client methods](#client-methods) below.

## Starting and ending a session by calling server methods

See the SignalR Client [documentation about calling server
methods](https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client#call-hub-methods-from-client)
to manage your session. For example, once connected to the hub, you must call
[StartOrRecoverSession](/services/playfab/multiplayer/real-time-messages/server-methods/start-or-recover-session) to receive a
`connection handle` that you can use to [subscribe to
resources](/services/playfab/multiplayer/real-time-messages/subscribing-to-resources). Similarly, once you're done with the
session, call [EndSession](/services/playfab/multiplayer/real-time-messages/server-methods/end-session).

## Subscribing to resources

Once a session has been started with
[StartOrRecoverSession](/services/playfab/multiplayer/real-time-messages/server-methods/start-or-recover-session), you can use
the `connection handle` returned to start [subscribing to
resources](/services/playfab/multiplayer/real-time-messages/subscribing-to-resources) and receiving messages.

## APIs

### Client methods

| Name                                                                                                                                    | Description                                                         |
| --------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
| [ReceiveMessage](/services/playfab/multiplayer/real-time-messages/client-methods/receive-message)                                       | Receive a message.                                                  |
| [ReceiveSubscriptionChangeMessage](/services/playfab/multiplayer/real-time-messages/client-methods/receive-subscription-change-message) | Receive a SubscriptionChangeMessage to track current subscriptions. |

### Server methods

| Name                                                                                                              | Description                 |
| ----------------------------------------------------------------------------------------------------------------- | --------------------------- |
| [StartOrRecoverSession](/services/playfab/multiplayer/real-time-messages/server-methods/start-or-recover-session) | Start or recover a session. |
| [EndSession](/services/playfab/multiplayer/real-time-messages/server-methods/end-session)                         | End the session.            |

#### Shared session methods

| Name                                                                                                                  | Description                                                                                                                                                                      |
| --------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [AddEntityToSession](/services/playfab/multiplayer/real-time-messages/server-methods/add-entity-to-session)           | For scenarios where multiple entities are signed in to the same device. Add an extra entity to the session, to receive messages for multiple local entities on a shared session. |
| [RemoveEntityFromSession](/services/playfab/multiplayer/real-time-messages/server-methods/remove-entity-from-session) | Remove an entity from the session.                                                                                                                                               |

## See also

* [Subscribing to resources](/services/playfab/multiplayer/real-time-messages/subscribing-to-resources)
* [Real-time messages for Lobby and Matchmaking APIs](/services/playfab/multiplayer/real-time-messages/overview)


## Related topics

- [ReceiveMessage SignalR client method](/services/playfab/multiplayer/real-time-messages/client-methods/receive-message.md)
- [ReceiveSubscriptionChangeMessage SignalR client method](/services/playfab/multiplayer/real-time-messages/client-methods/receive-subscription-change-message.md)
- [EndSession SignalR server method](/services/playfab/multiplayer/real-time-messages/server-methods/end-session.md)
- [Message real-time messages type](/services/playfab/multiplayer/real-time-messages/types/message.md)
- [StartOrRecoverSession SignalR server method](/services/playfab/multiplayer/real-time-messages/server-methods/start-or-recover-session.md)
