Course contentsShow
← Unity Game Development
Lesson 809 of 82812. Multiplayer and DeploymentFree lesson

Handling Matchmaking Tickets and Session Assignment

Monitor matchmaking ticket status and automatically transition players into a dedicated game session once a match is found.

In this lesson, you will learn how to monitor a matchmaking ticket's lifecycle and programmatically transition players into an active game session once the Unity Matchmaking service finds a match.

The Lifecycle of a Matchmaking Ticket

When you submit a ticket to the Unity Matchmaking service, the status begins as Searching. Your client needs to poll the service to check if the ticket has transitioned to Matched. Once the state is Matched, the service provides a MatchId and, depending on your configuration, connection details for a dedicated server (like a Relay allocation or Multiplay server IP).

Monitoring and Transitioning

To handle this, your game client must periodically query the status of the TicketId you received upon submission. Avoid blocking the main thread; instead, use an async task to poll the status every few seconds.

Once the status returns as Matched, you trigger your network client to connect to the provided connection string. Here is a simplified pattern:

async Task MonitorTicket(string ticketId)
{
    while (true)
    {
        var status = await MatchmakerService.Instance.GetTicketAsync(ticketId);
        
        if (status.Value == "Matched")
        {
            // Transition: Extract connection info from the ticket response
            string connectionString = status.MatchProperties.Players[0].Connection;
            StartNetworkClient(connectionString);
            break; 
        }
        
        await Task.Delay(3000); // Poll every 3 seconds
    }
}

Session Assignment Logic

When the ticket is marked as Matched, the matchmaking service effectively "assigns" the player to a session. If you are using Unity Relay, the match properties will contain the AllocationId. Your client uses this AllocationId to configure the RelayServerData, allowing the NetworkManager to establish the connection to the specific host or server provisioned for that match. By decoupling the ticket status check from the connection logic, you ensure that the player only attempts to join once the backend has confirmed that the server is ready to accept them, preventing premature connection attempts or "room not found" errors.

Key Takeaway: Monitoring a ticket involves polling the matchmaking service for a Matched status, then immediately using the returned connection metadata to point your NetworkManager toward the assigned game session.