Guide

How to make a Babylon.js game multiplayer

Updated 2026-11-08

Short answer: load Joinable's script, register your game with Joinable.game(), report your player from local(), and move one mesh per other player inside engine.runRenderLoop. Joinable runs the lobby, the countdown, live standings, results and rematch.

Which pattern fits: the same as three.js: full rounds (race or score) for anything with a finish line or a score, Pattern B for avatars in a shared world. Report plain numbers, never meshes or Vector3s.

Draw the other players from the remote(players) hook. Joinable calls it before each frame of any engine that runs on requestAnimationFrame, as engine.runRenderLoop does (checked with Babylon.js 7.40, even when the engine starts before Joinable loads). Keep the list and draw from it in your loop, as above. Reading Joinable.remote() in the loop gives the same list, but the self-test before getjoinable 0.2.3 fails a race game without the hook.

Full rounds

<script src="https://cdn.babylonjs.com/babylon.js"></script>
<script src="game.js"></script>
<script async src="https://relay.getjoinable.com/sdk.js" data-connect="invite"></script>

No key while you build. On localhost Joinable works without one, free, in rooms of up to 4 players. Before the game goes live on a real site, get a key free at getjoinable.com/app and add data-key="pk_…" to the tag. Set theme to your game's own colours and font too: Joinable's waiting room, countdown and results then look like part of your game (the kit's "Match the game's look").

const rivals = new Map();   // player id → mesh
let others = [];            // the other players, from Joinable's remote() hook
let racing = false, online = false;

engine.runRenderLoop(() => {
  const dt = engine.getDeltaTime() / 1000;
  if (racing || !online) drive(player, dt);
  for (const p of others) {
    if (!p.state) continue;                   // no position from them yet
    let m = rivals.get(p.id);
    if (!m) {
      m = kartTemplate.clone(`rival-${p.id}`);
      m.material = kartMaterial.clone(`rival-mat-${p.id}`);
      m.material.diffuseColor = BABYLON.Color3.FromHexString(p.color);
      rivals.set(p.id, m);
    }
    m.position.set(p.state.x, 0, p.state.y);
    m.rotation.y = -p.state.a;
    m.visibility = p.stale ? 0.25 : p.contact === 0 ? 0.5 : 1;
  }
  scene.render();
});

(window.JoinableQ = window.JoinableQ || []).push((Joinable) => {
  onlineButton.isVisible = true;                       // a Babylon GUI button, hidden until Joinable is here
  onlineButton.onPointerUpObservable.add(() => Joinable.open());
  Joinable.game({
    profile: 'race',
    name: 'My Racer',
    players: { min: 2, max: 8 },
    theme: { accent: '…', panel: '…', text: '…', font: '…' }, // your game's own colours and font
    public: true,
    start({ seed }) { online = true; racing = false; resetRace(seed); },   // seed every random choice
    go() { racing = true; },
    local() {
      const v = player.velocity;
      return { x: player.position.x, y: player.position.z, a: -player.rotation.y, vx: v.x, vy: v.z, lap: player.lap, progress: player.progress };
    },
    remote(players) { others = players; },             // before every frame
    stop() {
      online = racing = false;
      others = [];
      for (const m of rivals.values()) m.dispose();
      rivals.clear();
      showMenu();
    },
  });
});

window.Joinable?.finish({ time: raceTime });           // when the player crosses the line

Test it

npx getjoinable test http://localhost:8080/ --channel chrome

Where this stops working

Same limits as three.js: Havok and Ammo physics aren't deterministic across devices, so rivals are predicted; big shared simulations need the lockstep profile and a deterministic simulation.

Copy the prompt for your coding agent or sign up free