Skip to main content
🎉 GP CONF 2026: WEB Games Market Conference 👾 September 23 | Details

Connection and Room Lifecycle

Multiplayer uses a GamePush channel as a room. All players connecting with the same channelId join the same multiplayer session.

Method List

Actions:

Properties:

Events:

Choose a Channel

Any channel can technically be used for multiplayer if:

  • all players are channel members;
  • the channel has available member slots;
  • join requests for a private channel have already been accepted;
  • all players connect using the same channelId.

In most games, create a separate channel for each lobby or match. The channel members then represent the players allowed to join that multiplayer room, while the channel privacy and capacity settings define the lobby access rules and maximum player count.

Avoid using a large public chat channel as a multiplayer room. Every channel member who connects with its channelId becomes part of the same multiplayer session.

See Manage Channels and Channel Members for channel creation, capacity, privacy, and membership settings.

Connect to a Room

Subscribe to lifecycle events, make sure the player is a channel member, and connect to the room:

const channelId = 123;

function onConnect({ success }) {
console.log('Connected:', success);
}

function onDisconnect({ reason }) {
console.log('Disconnected:', reason);
}

gp.multiplayer.on('connect', onConnect);
gp.multiplayer.on('disconnect', onDisconnect);

async function connectToRoom() {
const channel = await gp.channels.fetchChannel({ channelId });

if (!channel.isJoined) {
await gp.channels.join({ channelId });
}

await gp.multiplayer.connect({ channelId });
}

await connectToRoom();
warning

Joining a private channel may only create a join request. Call gp.multiplayer.connect() after the request has been accepted and the player has become a channel member.

Calling connect() while already connected throws an error.

Current Connection State

Use isConnected to check the connection and isHost to check the current player's role:

gp.multiplayer.isConnected;
gp.multiplayer.isHost;

The host is selected asynchronously. Use the role events to start and stop host-only logic:

gp.multiplayer.on('becameHost', startHostLogic);
gp.multiplayer.on('becamePeer', stopHostLogic);

The hostMigrated event provides both player IDs:

gp.multiplayer.on('hostMigrated', ({ oldHost, newHost }) => {
console.log(`Host changed from ${oldHost} to ${newHost}`);
});

Connected Players

gp.multiplayer.connectedPlayers;

Each item in connectedPlayers contains:

{
playerId: 123,
isHost: false,
ping: 42,
connectionStability: 1,
sessionDuration: 15000,
}

Network Statistics

The SDK exposes current network diagnostics:

const {
ping,
bufferSize,
bufferDelay,
} = gp.multiplayer.networkStats;
  • ping is the current player's measured latency in milliseconds.
  • bufferSize is the number of snapshots in the interpolation buffer.
  • bufferDelay is the time range covered by buffered snapshots.

These values are useful for a debug panel and connection quality indicators.

Handle Errors

gp.multiplayer.on('error:connect', (error) => {
console.error('Could not connect to multiplayer:', error);
});

gp.multiplayer.on('error:sendState', (error) => {
console.error('Could not send multiplayer state:', error);
});

gp.multiplayer.on('error:disconnect', (error) => {
console.error('Could not disconnect from multiplayer:', error);
});

Disconnect and Clean Up

Stop game loops before disconnecting:

async function leaveRoom() {
await gp.multiplayer.disconnect({ channelId });

gp.multiplayer.off('connect', onConnect);
gp.multiplayer.off('disconnect', onDisconnect);
}

disconnect() does nothing if the player is not currently connected.

Stay in Touch

Other documents of this chapter are available here. To get started with GamePush, see the Tutorials chapter.

GamePush Community Telegram: @gs_community.

For your suggestions e-mail: official@gamepush.com

We Wish you Success!