Home/Engines/Babylon.js multiplayer

engines · Babylon.js

Babylon.js multiplayer

Babylon.js ships physics, XR and a full scene graph. It does not ship a backend. Add one import and your scene gets rooms, players and a shared, validated state.

Free sandbox · $19.99/mo to go live · cancel anytime

why

Babylon.js multiplayer, hosted

Babylon's render loop and observables map naturally onto the SDK's events. Remote meshes are created on members, updated on peer, and your HUD reacts to state.watch. Nothing runs on a server you own.

code

A complete Babylon.js integration

import { Multiplayer } from 'https://multiplayerapi.com/sdk/mp.esm.min.js';

const mp = new Multiplayer({ key: 'mp_pk_...' });
await mp.login();
const room = await mp.quickmatch({ mode: 'coop', max_players: 6 });

const avatars = {};
room.on('peer', (p, from) => {
  const a = avatars[from] ??= BABYLON.MeshBuilder.CreateCapsule('p_' + from, { height: 1.8 }, scene);
  a.metadata = { target: new BABYLON.Vector3(p.x, p.y, p.z), yaw: p.yaw };
});
room.on('members', (list) => {
  const alive = new Set(list.map((m) => m.account_id));
  for (const id in avatars) if (!alive.has(id)) { avatars[id].dispose(); delete avatars[id]; }
});

scene.onBeforeRenderObservable.add(() => {
  for (const a of Object.values(avatars)) if (a.metadata) {
    a.position = BABYLON.Vector3.Lerp(a.position, a.metadata.target, 0.25);
    a.rotation.y = a.metadata.yaw;
  }
});

setInterval(() => {
  const p = player.position;
  room.send({ x: p.x, y: p.y, z: p.z, yaw: player.rotation.y });
}, 50);

// Co-op world: everyone may write, at a bounded rate
room.inc('world.resources.wood', -10);
room.state.watch('world.resources.*', (v, path) => ui.update(path, v));
architecture

What goes where

ChannelCallDeliveryUse it for
Directroom.send(data)peer-to-peer, best effort, zero server costpositions, aim, animation
Stateroom.set(path, value)server-validated, kept for the match, reaches late joinershealth, score, whose turn
Eventsroom.emitEvent(topic, data)ordered, guaranteed, not persistedchat, rematch, one-shots
  • Use preset coop for shared worlds: rules such as "world.**": {"write":"any","rate":10} let everyone build while capping spam.
  • For Babylon XR, send head and hand poses on the direct channel exactly like the player transform.
Questions
Is there a Babylon.js-specific SDK?

No need: the JavaScript SDK has zero dependencies and works alongside Babylon.js as an ES module or a script tag.

Can I use Havok physics with it?

Yes. Run physics locally, stream resulting transforms peer-to-peer, and write only the outcomes that matter (health, score, ownership) to shared state.

Make your game online today

Free sandbox keys in one call, no account and no card. $19.99/month when you ship, for every game you make.

Free sandbox · $19.99/mo to go live · cancel anytime