Heartbeating a lobby

Lobbies are marked as inactive if they haven’t been updated* or sent a heartbeat request by the host in the last 30 seconds. Inactive public lobbies do not appear in query results**, and both public and private inactive lobbies will eventually be automatically deleted. Inactive lobbies can be reactivated by being updated or sent a heartbeat request by the host.

After 1 hour of inactivity, lobbies are considered expired. After a lobby expires, barring a new update or heartbeat, a lobby is subject to automatic deletion by the Lobby service. Upon deletion, a lobby is no longer available in queries or any APIs.

*Updates only count if they make a change to the lobby properties; this does not include changes to the host's player data, and does not include no-op updates.

**Inactive lobbies are excluded from queries by an automatically added filter based on a lobby's LastUpdated field, but inactive lobbies are still accessible in queries by passing in a custom query filter on the LastUpdated field. The Get Lobby API is not affected by inactive status.

The following code sample shows how to integrate a co-routine with lobby creation to periodically heartbeat a new lobby:

Copy
C#
async Task<Lobby> CreateLobbyWithHeartbeatAsync()
{
// Lobby parameters code goes here...
      // See 'Creating a Lobby' for example parameters
var lobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);

// Heartbeat the lobby every 15 seconds.
StartCoroutine(HeartbeatLobbyCoroutine(lobby.Id, 15));
return lobby;
}

IEnumerator HeartbeatLobbyCoroutine(string lobbyId, float waitTimeSeconds)
{
     var delay = new WaitForSecondsRealtime(waitTimeSeconds);
     while (true)
     {
         Lobbies.Instance.SendHeartbeatPingAsync(lobbyId);
         yield return delay;
     }
}

While hosts should periodically heartbeat their lobbies when in use, hosts should also be mindful of these lobbies and delete them afterwards. A very simple way to do this is to loop over created lobbies during shutdown.

The following code sample shows how to utilize a MonoBehavior’s OnApplicationQuit() function to delete created lobbies:

Copy
C#
ConcurrentQueue<string> createdLobbyIds = new ConcurrentQueue<string>();

void OnApplicationQuit()
{
     while (createdLobbyIds.TryDequeue(out var lobbyId))
     {
         Lobbies.Instance.DeleteLobbyAsync(lobbyId);
     }
}