Heartbeat a lobby
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.
Note: 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.
The main purpose of hiding inactive lobbies is to prevent players from continually finding/joining abandoned lobbies. Inactive public lobbies do not 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.
Note: Inactive lobbies are automatically excluded from queries using a filter on the LastUpdated
field. If you include a LastUpdated
filter it overrides the default filter and can include inactive lobbies in query results. The Get Lobby
API is not affected by inactive status.
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 is not 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 utilize a MonoBehavior
’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);
}
}