Skip to main content
πŸŽ‰ GP CONF 2026: WEB Games Market Conference πŸ‘Ύ September 23 | Details

Multiplayer Messages

Messages represent one-time actions that do not need to be stored and interpolated as state.

Method List​

Actions:

Events:

Use messages for events such as:

  • a player firing a weapon;
  • an explosion or visual effect;
  • an emote;
  • starting or restarting a round;
  • interaction with a map object.

Use player state or global state when late snapshots must still contain the value.

Receive Messages​

Register one handler and route messages by eventName:

function onMultiplayerMessage({
eventName,
senderId,
data,
timestamp,
}) {
if (eventName === 'shoot') {
createBullet({
ownerId: senderId,
x: data.x,
y: data.y,
direction: data.direction,
});
}

if (eventName === 'emote') {
showEmote(senderId, data.name);
}
}

gp.multiplayer.onMessage(onMultiplayerMessage);

The handler receives:

FieldDescription
eventNameMessage type defined by the game
senderIdID of the player who sent the message
dataArbitrary JSON-compatible payload
timestampMessage creation time

Send to Other Players​

With no target, the message is sent to all other players:

gp.multiplayer.sendMessage('shoot', {
x: player.x,
y: player.y,
direction: player.direction,
});

'all' can be passed explicitly:

gp.multiplayer.sendMessage(
'roundStarted',
{ round: 2 },
'all',
);

To send a message to one player, pass their playerId:

gp.multiplayer.sendMessage(
'privateEmote',
{ name: 'wave' },
targetPlayerId,
);

Process a Message Locally with Echo​

By default, the sender does not receive their own message. Use echo: true to run the same local handler immediately:

gp.multiplayer.sendMessage(
'explosion',
{
x: 320,
y: 180,
radius: 80,
},
{
target: 'all',
echo: true,
},
);

The sender receives the echoed event locally without waiting for a network round trip. Other players receive it through multiplayer normally.

This is useful when all clients, including the sender, should execute the same event handler.

Choose an Authority Model​

Messages transport an event but do not validate game rules. Decide which client is allowed to produce or confirm an event.

For host-authoritative actions, peers can send a request and the host can broadcast the accepted result:

function onMultiplayerMessage({ eventName, senderId, data }) {
if (eventName === 'requestOpenChest' && gp.multiplayer.isHost) {
if (canOpenChest(senderId, data.chestId)) {
openChestInGlobalState(data.chestId);

gp.multiplayer.sendMessage('chestOpened', {
chestId: data.chestId,
playerId: senderId,
});
}
}
}

gp.multiplayer.sendMessage('requestOpenChest', {
chestId: 'chest-7',
});

The durable result belongs in globalState; the messages represent the request and notification.

Unsubscribe​

Pass the same function reference used during subscription:

gp.multiplayer.offMessage(onMultiplayerMessage);

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!