›Multiplayer

Multiplayer with CudicMulti

A live opponent changes a game completely. CudicMulti gives any published game rooms over Cudic's websocket: players join anonymously (no account needed to play along), exchange small messages, and see each other in real time. Your game never touches a token — the host page mediates and the rooms are enforced server-side.

Cudic Cookbook · the API: CudicMulti.join / send / on / leave

The API, all of it

<script src="/cudic-multi.js"></script>
<script>
  var net = CudicMulti.join('arena');   // any room code — default 'main'

  net.on('open',  function () { … });   // joined the room
  net.on('peers', function (list) { … });   // [{ id, name }, …] including you
  net.on('msg',   function (m) { … });      // { from: peerId, data: your JSON }
  net.on('error', function (text) { … });   // e.g. playing locally, no relay
  net.on('close', function () { … });       // after leave() or lost relay

  net.send({ x: 10, y: 4 });            // any JSON value ≤ 8 KB; sent before
                                        // 'open' is queued, not dropped
  net.id;      // your peer id this session
  net.peers;   // the current peer list snapshot
  net.leave(); // leave the room
</script>

send queues your messages until open fires, so "join then immediately broadcast the state" is always safe. Limits are real but generous: 32 peers per room, 8 KB per message, with per-connection joins and message-rate caps to stop floods.

Names come from the server

A peers entry's name is the player's profile display name if signed in, or Guest otherwise. A client can't claim someone else's name or id — the server replaces attempts. Build your scoreboard on name, not on anything the client typed.

A two-player chase, minimal

Each frame, every player reports their position; everyone draws everyone. Paste into index.html, Run in Studio (Studio's preview joins a <project>:preview room, so it never collides with the published game's rooms):

<!DOCTYPE html>
<html><head><meta charset="utf-8"><style>html,body{margin:0;height:100%;
background:#101d26;overflow:hidden}canvas{display:block;width:100vw;height:100vh}
</style></head><body><canvas id="c" width="800" height="450"></canvas>
<script src="/cudic-multi.js"></script><script>
var x = c.getContext('2d');
var players = {};          // peerId → { x, y, name }
var me = { x: 400, y: 225 };

var net = CudicMulti.join('tag');
net.on('peers', function (list) {
  list.forEach(function (p) {
    if (!players[p.id]) players[p.id] = { x: 400, y: 225, name: p.name };
  });
  x.fillStyle = '#101d26'; x.fillRect(0, 0, c.width, c.height);  // full redraw
});
net.on('msg', function (m) {
  var p = players[m.from];
  if (p && m.data && typeof m.data.x === 'number') { p.x = m.data.x; p.y = m.data.y; }
});
net.on('error', function (t) { x.fillStyle = '#e06c5a'; x.fillText(t, 12, 24); });

addEventListener('mousemove', function (e) {
  var r = c.getBoundingClientRect();
  me.x = Math.round((e.clientX - r.left) / r.width * c.width);
  me.y = Math.round((e.clientY - r.top) / r.height * c.height);
});

setInterval(function () { net.send({ x: me.x, y: me.y }); }, 50);

(function draw() {
  x.fillStyle = '#101d26'; x.fillRect(0, 0, c.width, c.height);
  x.fillStyle = '#ff8c1a'; x.fillRect(me.x - 10, me.y - 10, 20, 20);
  for (var id in players) if (id !== net.id) {
    x.fillStyle = '#7ee0c0'; x.fillRect(players[id].x - 10, players[id].y - 10, 20, 20);
    x.fillStyle = '#cfe8dd'; x.fillText(players[id].name, players[id].x - 14, players[id].y - 16);
  }
  requestAnimationFrame(draw);
})();
</script></body></html>

Open the preview, then open the published (or second preview) page in another tab — move your mouse in one window and the other chases along. That's the whole transport story: position messages one way, net.id to skip drawing yourself.

Design rules for rooms

  • Send state, not trust. Messages are data, not commands from gods — validate on arrival (clamp speeds, bounds-check positions) the same way you'd never trust a save file.
  • Rooms are anonymous by design so guests can play along; signed-in names show through. Let guests join the fun — it grows your game's audience.
  • One room per game per context. view pages mediate real rooms for the published game; Studio previews use their own preview-keyed rooms so your playtesting can't disturb players.
  • Reconnect cheap. If the relay drops mid-session, close fires — re-join with the same join(room) and resync state from whoever responds.

Publish the result and watch the gallery — games with live "playing now" counts pull a crowd, and a lobby of two is a party. Post your room story in the Showcase when it works.