Documentation

Support

Multiplayer Services SDK

All Services

Multiplayer Services SDK

Session host migration

Learn about changing session ownership, including host election and data migration.
Read time 2 minutesLast updated 14 hours ago

In the sessions system, migration involves two aspects: host election, and data migration. The session system exposes to notify you about changes in session ownership and migration status:
  • ISession.SessionHostChanged
    is raised when a new host is elected.
  • ISession.SessionMigrated
    is raised when data migration has completed.

Host election

Host election allows changing the session’s host. The current host can start this by updating the session’s assigned host ID. For the change to take effect, save all session properties so the update propagates to all participants.

Data migration

Use host data migration to keep a client-hosted session running after the host is lost. This supports both voluntary and involuntary interruptions, such as network disconnections, power failures, or the host quitting the application. Enable host data migration for a session by calling
WithHostMigration
on the session options when creating or joining a session. Data migration requires an
IMigrationDataHandler
implementation, which defines how network-synchronized data is captured and applied. A default handler is provided for Netcode for Entities (minimum version 1.7.0). You can also supply a custom
IMigrationDataHandler
to meet specific needs.
Use the
WithHostMigration
overloads to:
  • Configure the interval for automatic Netcode snapshot generation.
  • Set a timeout for snapshot uploads.
  • Provide a custom
    IMigrationDataHandler
    if
    EntitiesMigrationDataHandler
    doesn’t fit your use case.
  • Defaults: a 3-second snapshot interval and a 5-second upload timeout.
Hosts can also manage migration snapshots manually using
GetHostMigrationDataAsync
and
SetHostMigrationDataAsync
. These methods are primarily intended for custom
IMigrationDataHandler
implementations. Internally, they use the Lobby service to store and retrieve network snapshots and are automatically invoked by the NetworkModule when a data migration is detected.

Using Relay

Added optional parameter
preserveRegion
to
RelayOptions
to configure relay reallocation behavior during host migration. Setting this parameter to true saves the region of the first relay allocation and reuses it when a relay server is reallocated during host migration. Configure RelayOptions via the
WithRelayNetwork
or
StartRelayNetwork
extension methods, depending on your use case.

Examples

Example: Creating a custom IMigrationData

Implement a data migration handler.
public class CustomDataHandler : IMigrationDataHandler{ public byte[] Generate() { // implement a process to serialize data based on the network component in the scene / world return new byte[42]; } public void Apply(byte[] migrationData) { // implement a process to de-serialize data and apply in current scene / server world Assert.AreEqual(42, migrationData.Length); }}
Use the custom implementation with the sessions.
public Task CreateOrJoinSessionWithDataMigartionEnabled(string sessionId){ var sessionOptions = new SessionOptions().WithHostMigration(migrationDataHandler: new CustomDataHandler()); var session = await MultiplayerService.Instance.CreateOrJoinSessionAsync(sessionId, options)}

Example: Electing a new Host

The following code snippet demonstrates how to elect a given
player
by its ID as the new host:
public Task ElectPlayerAsHostAsync(string playerId){ _session.AsHost().Host = player.Id; return _session.AsHost().SavePropertiesAsync();}
After setting the
session
's host ID, save the changes to take effect.