The multiplayer API is made up of two events, one for sending data (see postMessage #broadcast-event), and another for listening to data sent by connected players (see addEventListener #multiplayer-events).
Here is a minimal example that sends a Hello message for players that have joined.
addEventListener("message", ({ data: { type, payload } }) => {
if (type === "oncanvas") {
// send data
const { sessionId } = payload;
postMessage({
type: "broadcast",
payload: {
message: "Hello from " + sessionId,
},
});
} else if (type === "onlisten") {
// get data
const { message } = payload;
postMessage({
type: "consolelog",
payload: message,
});
}
});
Check out the Multiplayer Jumping Circles codetoy to see a working example.