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.
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));What goes where
| Channel | Call | Delivery | Use it for |
|---|---|---|---|
| Direct | room.send(data) | peer-to-peer, best effort, zero server cost | positions, aim, animation |
| State | room.set(path, value) | server-validated, kept for the match, reaches late joiners | health, score, whose turn |
| Events | room.emitEvent(topic, data) | ordered, guaranteed, not persisted | chat, rematch, one-shots |
- Use preset
coopfor 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.
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