# Advanced matchmaking

> Use additional rules and parameters to matchmake players on multiple variables.

## Find a session using Unity Matchmaker service

Use the [Unity Matchmaker](/matchmaker.md) service to have a player join the best-suited session using more advanced [rules](/matchmaker/matchmaking-rules.md).

Matches can be tuned for network quality using [Quality of Service (QoS)](/matchmaker/advanced-topics-qos.md) rules on the pool configuration. This type of rule ensures that players in the match don't exceed a specified latency to the host or server.
For a given match, these rules also select the most suitable region in which to allocate a server or relay.

> **Note:**
>
> The Multiplayer Services SDK automatically performs QoS measurements, and includes the results with the matchmaking request.

The following example demonstrates how to find and join a session using Matchmaker:

```cs
var matchmakerOptions = new MatchmakerOptions
{
    QueueName = "Friendly"
};

var sessionOptions = new SessionOptions()
{
    MaxPlayers = 2
}.WithDirectNetwork();

var matchmakerCancellationSource = new CancellationTokenSource();

ISession session = await MultiplayerService.Instance.MatchmakeSessionAsync(matchmakerOptions, sessionOptions, matchmakerCancellationSource.Token);
```

For Matchmaker to find a session for the player, the user is expected to configure the matchmaking [rules](/matchmaker/matchmaking-rules.md).

> **Note:**
>
> To test this code,
> create a [queue](/matchmaker/advanced-topics-queues-pools.md#queues) named `Friendly`
> and use the default pool.

### Network choice when matchmaking

* The recommended best practice is to use `WithDirectNetwork()` when the Matchmaker pool is configured with your preferred hosting provider.
* `WithDirectNetwork()` instructs clients to use direct network connections to the allocated server's IP and port.
* The recommended best practice is to use `WithRelayNetwork()` or `WithDistributedAuthorityNetwork()` when the Matchmaker pool is configured for client-hosted (peer-to-peer) games.

### Cancel matchmaking

If the player wants to stop the matchmaking process, use the following code:

```cs
matchmakerCancellationSource.Cancel();
```

This indicates to the Multiplayer Services SDK to delete the matchmaking ticket that was sent in the
background to the service, and stop the search for a session.

### Matchmaking results

The session also provides access to
the [Matchmaking Results](/matchmaker/matchmaker-ticket-flow.md) with the information
about the match and the players that are going to connect to the session (their teams and their data).

Some information, such as team breakdown and custom ticket data, is only available through matchmaking results.
The results also include the list of expected players,
which is handy as it is available immediately and authoritatively before the players join the session and connect.

You can access the matchmaking results with the following code:

```cs
var matchmakingResults = session.GetMatchmakingResults();
```

## Additional resources

* [ISession interface](https://docs.unity3d.com/Packages/com.unity.services.multiplayer@latest/index.html?subfolder=/api/Unity.Services.Multiplayer.ISession.html)
* [MatchmakerOptions](https://docs.unity3d.com/Packages/com.unity.services.multiplayer@latest/index.html?subfolder=/api/Unity.Services.Multiplayer.MatchmakerOptions.html)
* [Matchmaker overview](/matchmaker.md)
* [Matchmaker Rules samples](/matchmaker/rules-sample.md)
* [QuickJoinOptions](https://docs.unity3d.com/Packages/com.unity.services.multiplayer@latest/index.html?subfolder=/api/Unity.Services.Multiplayer.QuickJoinOptions.html)
* [Unity Lobby service](/lobby.md)
