The Lobby service plays an essential role in setting up multiplayer gaming sessions. One important aspect of managing lobbies is maintaining their active state, which is achieved using the concept of "heartbeats". This guide will take you through the basics of lobby heartbeats, why they are crucial, and how to implement them in your Unity project.
Why do we need Lobby Heartbeats?
Lobby is built on top of stateless networking protocols, meaning it doesn't inherently know whether a connection is still alive or not. To determine if a lobby host is still online, we employ heartbeats, where the host sends an occasional "keep-alive" message to the service.
If the Lobby service doesn't receive a heartbeat from a host within 30 seconds, the lobby is marked as inactive and won't appear in query results. The lobby can be reactivated if a heartbeat is received within an hour. However, if a lobby fails to send a heartbeat for over an hour, it's considered expired. It can't be reactivated and is automatically deleted by the service.
This system is vital for maintaining efficient networking, as it helps to prevent players from continually finding and attempting to join abandoned lobbies.
Heartbeating a Lobby in Unity
To heartbeat a lobby in Unity, you need to periodically send a heartbeat request to the Lobby service. Below is an example of how you can create a new lobby and integrate a coroutine to send a heartbeat every 15 seconds.
async Task<Lobby> CreateLobbyWithHeartbeatAsync()
{
string lobbyName = "test lobby";
int maxPlayers = 4;
CreateLobbyOptions options = new CreateLobbyOptions();
// Create the lobby
var lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
// Start the coroutine to 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;
}
}
Deleting Lobbies After Use
While keeping your lobbies alive with heartbeats when they are in use, it's also essential to delete them after they've served their purpose. Here's a simple example of how to delete all created lobbies during application shutdown using the OnApplicationQuit() function.
ConcurrentQueue<string> createdLobbyIds = new ConcurrentQueue<string>();
void OnApplicationQuit()
{
while (createdLobbyIds.TryDequeue(out var lobbyId))
{
LobbyService.Instance.DeleteLobbyAsync(lobbyId);
}
}
This function ensures that any lobbies created during the session are deleted when the application quits, preventing any unnecessary lobby resources from remaining.