Skip to content

Multiplayer

Updated

Multiplayer lets several people share one world at the same time. When two visitors open the same scene, they meet in a room on Arena, the room server: they see each other’s avatars, the crate one of them pushes moves for everyone, the scoreboard reads the same on every screen, and the chat reaches the whole room. As a scene author you decide which objects are shared and what about them travels; MIRA does the rest. The same scene still runs on its own when there is no room, so you can build and test alone and add people later.

  • Room. A shared session. Everyone connected to the same room sees the same shared objects. When the project has networking on, a scene joins its room while it loads, so its scripts start already inside it; otherwise a script joins with Network.connect().
  • Peer. One connected client: one browser tab, phone or headset. Each peer has an id in the room, Network.peerId.
  • Shared object. An entity, or a plain object, that the room knows about. An entity becomes shared when it has a Networkable component.
  • Owner. Every shared object has exactly one owner at a time: the peer that simulates it, runs its physics and movement, and sends its state. Only the owner’s changes reach the others.
  • Proxy. The copy every other peer holds. A proxy does not simulate; it follows the state its owner sends, shown a moment behind and blended smoothly.
  • Trait. One thing about an object that travels: its transform (position, rotation, scale), its rigidbody velocities, a component’s props (a light’s colour, a slider’s value), or a value of your own. You choose traits with Networkable Trait components or from a script.
  • Partition. The group an object belongs to, normally its scene. A peer only receives the objects of the scenes it has loaded.
  • Command. An action a peer asks the room to carry out: send a reaction, raise a hand, lock the room. The room’s rules decide who may run each one.
  • Persistence. Shared state lives as long as the room keeps it. To store data that outlives rooms and sessions, such as a high score or a guest book, use a Persistable.
Arena relays object state between the runtimes in a room Three runtime clients connect to Arena. The owner of an object sends its state; Arena checks what each person may do, relays state to the people who can see the object, and keeps persistent objects in storage. Arena never simulates. Runtime · owner simulates the object Runtime · peer shows a copy Runtime · peer too far away: nothing sent Arena room one owner per shared object sends state to who can see it checks what each person may do never simulates Storage persistent objects state state actions

Arena sits between the people in a room. Read the diagram from left to right:

  1. The owner simulates. The peer that owns an object moves it, runs its physics and its scripts, and sends its traits to Arena only when they change.
  2. Arena relays state to the people who can see it. A peer receives an object when it has loaded that object’s scene (its partition) and, when spatial interest is on, when the object is near it. A peer that is too far away is sent nothing for that object.
  3. Arena never simulates. It does not run physics or scripts. It keeps the record of each object, decides who owns it (ownership claims are served in the order they arrive), and checks what each person may do: every command is checked against the room’s rules before it runs.
  4. Persistent objects are kept. An object marked persistent stays in the room after everyone leaves, and the next visitor finds it where it was left.

What this means for your scripts:

  • Check ownership before changing shared state. A change made on a proxy stays local and is overwritten by the owner’s next update. Read isOwner, or call requestOwnership() first. Grabbing, selecting or dragging an object takes ownership automatically.
  • Every peer runs the same scripts. A Behavior runs on each peer’s copy of the scene. Use ownership to decide which peer acts, so that ten people do not all spawn the same projectile.
  • Without a room, everything is local. With no connection, every peer owns its own copy and the scene plays normally. You do not need separate code paths.
  • Nothing replicates while you edit. Sharing happens in play only.

This walkthrough shares a crate that anyone can grab and move. Its colour changes for everyone each time someone takes it.

  1. Add a primitive mesh named Crate to the scene, with a Standard Material and a Grabbable so people can drag it.

  2. Add a Networkable component to the Crate. This makes it a shared object. Leave ownership on transfer so anyone can take it.

  3. Add a Networkable Trait with trait set to transform, so its position and rotation travel, and a second one set to standard-material, so its colour travels too.

  4. Add a Signal Emitter to the Crate with the signal crate/net. The Networkable sends its events (ownership changes, peers joining) through it.

  5. Add a Behavior entity with this script:

    SharedCrate.jx
    #pragma lifecycle(startup)
    const COLORS = ['#f97316', '#22c55e', '#3b82f6', '#e11d48', '#eab308'];
    let crate;
    let net;
    let material;
    function startup() {
    crate = findEntity('Crate');
    net = crate.getComponent(NetworkableComponent);
    material = crate.getComponent(StandardMaterialComponent);
    log(Network.connected ? `In room ${Network.room} as ${Network.peerId}` : 'Playing alone');
    on('crate/net', (event) => {
    if (event.eventType === EventType.Networkable.OwnershipChange && event.data.isOwner) {
    // Only the new owner writes, and its change travels to everyone.
    const color = COLORS[Math.floor(Math.random() * COLORS.length)];
    material.setProps({ color });
    log('I took the crate');
    }
    if (event.eventType === EventType.Networkable.PeerJoin) {
    log(`Someone joined: ${event.data.peerId}`);
    }
    });
    }
  6. Press Play in two browser tabs on the same room. Drag the crate in one tab: it moves in the other, and it changes colour each time the other tab grabs it.

What happens: grabbing the crate asks Arena for its ownership. When it is granted, the Networkable sends Networkable::ownership-change with isOwner: true to the new owner, whose script picks a colour. Because the owner made the change and standard-material is a trait, the colour reaches every peer. The transform trait carries the drag.

If you want to… Use Read
Share an entity placed in the scene A Networkable with Networkable Traits Networkable
Share objects or values from a script, or spawn objects for everyone Network.replicate, Network.define and Network.spawn Network
Know when you join, leave or someone arrives Networkable events through a Signal Emitter Network
Lock the room, send reactions, moderate Network.command and NetworkCommand Commands
Keep data between sessions A Persistable Persistence
Show who is in the room, raise a hand Participants Participants
Send and read chat messages Chat Chat
List or choose the account’s avatars Avatars Avatars
Publish a camera, microphone or generated video Streams Live streams
GlobalWhat it is
NetworkReplicate objects and traits: replicate(object, options), unreplicate, of, define(kind, build, setup), spawn(kind, data, options), subscribe(partition), unsubscribe(partition); connected, peerId, peers, peerCount, interestCenter.
NetworkTraitTypeEvery trait a Networkable can replicate: traits: [NetworkTraitType.Transform, NetworkTraitType.PointLight].
NetworkCommandEvery command the room server defines: Network.command(NetworkCommand.ReactionSend, { reaction: '👍' }).
Participants
ChatOptional host chat service; explicit connect respects the Play/drawn-scene gate.
AvatarsAccount avatar operations supplied by the host.
StreamsLive streams by name, for Media Stream components: create(id, { video, audio }) then writeVideoFrame / writeAudioFrame, publish(id, Input.camera.stream()), ids(), has(id), on(id, live => …). Optional RTC: room.state, room.subscribe, room.connect/disconnect, and explicit await room.publish(source) / await room.unpublish(source) for mic, camera and screen.
EventTypeEvery event type a script can hear, by who sends it: compare them rather than typing the strings.
ComponentTypeWhat it does
Networkablecomp-networkableShares the entity with everyone in the room.
Networkable Traitcomp-networkable-traitOne thing its Networkable replicates to everyone in the room, and how often.
Topic Description
Networkable The Networkable and Networkable Trait components: owners and proxies, traits, frequencies, scenes and spatial interest.
Network The Network global: the session, replicating entities and plain objects, custom traits, spawning, partitions and connection events.
Commands Room actions with Network.command, permissions with can, relayed commands with onCommand, and refusals.
Participants Who is in the room, who is speaking, and raising a hand.
Chat General, direct and group chat from a script.
Avatars Listing, selecting and managing the account’s avatars.
Live streams Publishing cameras, microphones and generated media, and playing them on entities.