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.
Key concepts
Section titled “Key concepts”- 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), itsrigidbodyvelocities, 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.
How it works
Section titled “How it works”Arena sits between the people in a room. Read the diagram from left to right:
- 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.
- 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.
- 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.
- 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 callrequestOwnership()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.
Your first shared object
Section titled “Your first shared object”This walkthrough shares a crate that anyone can grab and move. Its colour changes for everyone each time someone takes it.
-
Add a primitive mesh named Crate to the scene, with a Standard Material and a Grabbable so people can drag it.
-
Add a Networkable component to the Crate. This makes it a shared object. Leave
ownershipontransferso anyone can take it. -
Add a Networkable Trait with
traitset totransform, so its position and rotation travel, and a second one set tostandard-material, so its colour travels too. -
Add a Signal Emitter to the Crate with the signal
crate/net. The Networkable sends its events (ownership changes, peers joining) through it. -
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}`);}});} -
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.
Your first shared object: a grabbable crate with a Networkable and transform and material traits; whoever takes it gives it a new colour for everyone.
Drag the crate. The preview has no Arena room, so the log says you are playing alone; in a published world, open it in two tabs to see the crate move and change colour in both.
Choosing
Section titled “Choosing”| 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., Network. 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. and Network |
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 |
In scripts
Section titled “In scripts”| Global | What it is |
|---|---|
Network | Replicate 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. |
NetworkTraitType | Every trait a Networkable can replicate: traits: [NetworkTraitType.Transform, NetworkTraitType.PointLight]. |
NetworkCommand | Every command the room server defines: Network.command(NetworkCommand.ReactionSend, { reaction: '👍' }). |
Participants | |
Chat | Optional host chat service; explicit connect respects the Play/drawn-scene gate. |
Avatars | Account avatar operations supplied by the host. |
Streams | Live 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. |
EventType | Every event type a script can hear, by who sends it: compare them rather than typing the strings. |
Components
Section titled “Components”| Component | Type | What it does |
|---|---|---|
| Networkable | comp-networkable | Shares the entity with everyone in the room. |
| Networkable Trait | comp-networkable-trait | One thing its Networkable replicates to everyone in the room, and how often. |
In this section
Section titled “In this section”| 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., 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. |