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:
gp.multiplayer.connect()- connect to a multiplayer room. +1 Requestgp.multiplayer.disconnect()- disconnect from the current room. +1 Request
Properties:
gp.multiplayer.isConnected- check whether the player is connected. FREEgp.multiplayer.isHost- check whether the current player is the host. FREEgp.multiplayer.connectedPlayers- get connected players. FREEgp.multiplayer.networkStats- get current network diagnostics. FREE
Events:
connectanddisconnect- track the connection lifecycle.playerJoinedandplayerLeft- track room members.becameHost,becamePeer, andhostMigrated- track host role changes.error:connect,error:sendState, anderror:disconnect- handle multiplayer errors.
Choose a Channel
Multiplayer uses GamePush channels. To play with other channel members, a player must be a member of that channel.
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.
Choose a hub
Channels, including multiplayer rooms, are hosted on the RU, EU, NA, and APAC hubs. To find an existing room before connect, use gp.channels.fetchChannels.
The location filter in fetchChannels limits the search to one hub. You can search with:
location: gp.player.locationto use the hub recommended for the player based on the country of their request (available after player sync);location: 'AUTO'to have the server choose a hub based on the country of the current request and return its channels;location: 'RU'to search on the specificRUhub;- no
locationfilter to search across all hubs.
Developers can use this search to implement region-based matchmaking or a screen listing channels available to the player. If no suitable rooms are found, the game decides what to do next: create a room, tell the player that no rooms are available, or search on other hubs.
In the channel template settings, you can select a fixed hub in the Location field. All channels created from that template will be hosted on the selected hub.
The filter is available in the JavaScript SDK. See Fetch List of Channels.
const { items } = await gp.channels.fetchChannels({
tags: ['lobby'],
location: gp.player.location, // or 'AUTO', 'RU', 'EU', 'NA', 'APAC'
limit: 20,
});
const channel = items.find((item) => item.membersCount < item.capacity) || items[0];
if (!channel) {
throw new Error('No lobby in this region');
}
if (!channel.isJoined) {
await gp.channels.join({ channelId: channel.id });
}
await gp.multiplayer.connect({ channelId: channel.id });
Connect to a Room
Subscribe to lifecycle events, make sure the player is a channel member, and connect to the room:
- JavaScript
- Construct 3
- Unity
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();
Legend
System
GP_Channels
System›Construct 3 System object
GP_Channels›GamePush Channels plugin
const int channelId = 123;
void OnConnect(GP_Data data)
{
Debug.Log("Connected: " + data.Data);
}
void OnDisconnect(GP_Data data)
{
Debug.Log("Disconnected: " + data.Data);
}
async void ConnectToRoom()
{
GP_Multiplayer.on("connect", OnConnect);
GP_Multiplayer.on("disconnect", OnDisconnect);
await GP_Multiplayer.connect(new MultiplayerChannelQuery
{
channelId = channelId
});
}
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:
- JavaScript
- Construct 3
- Unity
gp.multiplayer.isConnected;
gp.multiplayer.isHost;
Legend
GP_Channels
GP_Channels›GamePush Channels plugin
GP_Multiplayer.isConnected;
GP_Multiplayer.isHost;
The host is selected asynchronously. Use the role events to start and stop host-only logic:
- JavaScript
- Construct 3
- Unity
gp.multiplayer.on('becameHost', startHostLogic);
gp.multiplayer.on('becamePeer', stopHostLogic);
Legend
GP_Channels
GP_Channels›GamePush Channels plugin
GP_Multiplayer.on("becameHost", StartHostLogic);
GP_Multiplayer.on("becamePeer", StopHostLogic);
The hostMigrated event provides both player IDs:
- JavaScript
- Construct 3
- Unity
gp.multiplayer.on('hostMigrated', ({ oldHost, newHost }) => {
console.log(`Host changed from ${oldHost} to ${newHost}`);
});
Legend
GP_Channels
GP_Channels›GamePush Channels plugin
void OnHostMigrated(GP_Data data)
{
Debug.Log("Host migrated: " + data.Data);
}
GP_Multiplayer.on("hostMigrated", OnHostMigrated);
Connected Players
- JavaScript
- Construct 3
- Unity
gp.multiplayer.connectedPlayers;
Legend
System
Text
GP_Channels
System›Construct 3 System object
Text — Object Text (Text)
GP_Channels›GamePush Channels plugin
GP_Data connectedPlayers = GP_Multiplayer.connectedPlayers;
Debug.Log(connectedPlayers.Data);
Each item in connectedPlayers contains:
{
playerId: 123,
isHost: false,
ping: 42,
connectionStability: 1,
sessionDuration: 15000,
}
Network Statistics
The SDK exposes current network diagnostics:
- JavaScript
- Construct 3
- Unity
const {
ping,
bufferSize,
bufferDelay,
} = gp.multiplayer.networkStats;
Legend
System
Text
System›Construct 3 System object
Text — Object Text (Text)
GP_Data networkStats = GP_Multiplayer.networkStats;
Debug.Log(networkStats.Data);
pingis the current player's measured latency in milliseconds.bufferSizeis the number of snapshots in the interpolation buffer.bufferDelayis the time range covered by buffered snapshots.
These values are useful for a debug panel and connection quality indicators.
Handle Errors
- JavaScript
- Construct 3
- Unity
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);
});
Legend
GP_Channels
Text
GP_Channels›GamePush Channels plugin
Text — Object Text (Text)
void OnMultiplayerError(GP_Data error)
{
Debug.LogError(error.Data);
}
GP_Multiplayer.on("error:connect", OnMultiplayerError);
GP_Multiplayer.on("error:sendState", OnMultiplayerError);
GP_Multiplayer.on("error:disconnect", OnMultiplayerError);
Disconnect and Clean Up
Stop game loops before disconnecting:
- JavaScript
- Construct 3
- Unity
async function leaveRoom() {
await gp.multiplayer.disconnect({ channelId });
gp.multiplayer.off('connect', onConnect);
gp.multiplayer.off('disconnect', onDisconnect);
}
Legend
Button
GP_Channels
System
GamePush
Button — Object Button (Button)
GP_Channels›GamePush Channels plugin
System›Construct 3 System object
GamePush›GamePush plugin
async void LeaveRoom()
{
await GP_Multiplayer.disconnect(new MultiplayerChannelQuery
{
channelId = 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!