NetStack

Events

Events are named messages that Functions broadcast to everyone in a room.

Broadcasting from a Function

ctx.room.broadcast("itemUsed", { playerId: ctx.player.id, itemId: args.itemId });

The event name starts with a letter and uses letters, numbers and underscores, up to 48 characters. The data can be any JSON value up to 16 KB. A single call can broadcast up to 50 events.

Events are only sent if the Function finishes successfully. If it throws, nothing is broadcast.

Listening on the client

client.on("event", ({ name, data }) => {
  if (name === "itemUsed") playUseAnimation(data.playerId, data.itemId);
});

Property changes

Property updates are separate from events. Listen for propertyChange and propertyDelete if you want to react to state changing rather than to something happening:

client.on("propertyChange", ({ scope, playerId, key, value }) => {
  if (scope === "room" && key === "round") showRound(value);
});

Choosing between them

Use a property when the state should persist and be readable by late joiners, such as the current round. Use an event for something that happened once, such as an explosion. A late joiner receives current properties in the room snapshot but does not receive past events.