Documentation

Support

Lobby

Lobby

Heartbeat a lobby

Send heartbeat requests to keep a lobby active and prevent it from being marked as inactive or deleted.
Read time 2 minutesLast updated 15 hours ago

Lobbies are marked as inactive if they haven’t been updated or sent a heartbeat request by the host within a given duration. The default active lifespan of a lobby is 30 seconds. You can configure the active lifespan for lobbies in your project. The main purpose of hiding inactive lobbies is to prevent players from continually finding/joining abandoned lobbies. Inactive public lobbies don't appear in query results nor are they joinable through ‘Quick Join’, and both public and private inactive lobbies are eventually automatically deleted. Inactive lobbies can be reactivated by being updated or sent a heartbeat request by the host. After 1 hour of inactivity, a lobby is considered expired. An expired lobby may be deleted at any time. Upon deletion, a lobby is no longer available in queries or any APIs. This duration isn't configurable. You may reactivate an expired lobby by updating it or sending a heartbeat. The following code sample shows how to integrate a co-routine with lobby creation to periodically heartbeat a new lobby: C#
async Task<Lobby> CreateLobbyWithHeartbeatAsync(){ string lobbyName = "test lobby"; int maxPlayers = 4; CreateLobbyOptions options = new CreateLobbyOptions(); // Lobby parameters code goes here... // See 'Creating a Lobby' for example parameters var lobby = await LobbyService.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) { LobbyService.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 afterward. One way to do this is to loop over created lobbies during shutdown. The following code sample shows how to use a
MonoBehaviour
’s
OnApplicationQuit()
function to delete created lobbies:
C#
ConcurrentQueue<string> createdLobbyIds = new ConcurrentQueue<string>();void OnApplicationQuit(){ while (createdLobbyIds.TryDequeue(out var lobbyId)) { LobbyService.Instance.DeleteLobbyAsync(lobbyId); }}

Heartbeat a lobby • Lobby • Unity Docs