Multiplayer Server
📔 Note: The Multiplayer Server was previously called the
Authoritative Server. Only the name changed, the feature is the same. The SDK branch to install is still named
.
IMPORTANT: Always notify the user and ask them if they want to proceed before adding it to the scene. Mention that it requires installing the
branch instead of the standard SDK.
Build multiplayer Decentraland scenes where a
headless server controls game state, validates changes, and prevents cheating. The same codebase runs on both server and client, with the server having full authority. Decentraland hosts and deploys the server automatically. For basic CRDT multiplayer (no server), see the
skill instead.
Setup
You
must use
npm install @dcl/sdk@auth-server
and
npm install @dcl/js-runtime@auth-server
— the standard
does NOT include authoritative server APIs.
"authoritativeMultiplayer": true
at the
root of
is what enables the headless server (without it the scene runs as ordinary serverless CRDT and
never returns
), but you do
not add it manually: the
sdk-commands
auto-adds it on every build and preview (
writes
authoritativeMultiplayer: true
to
via
, only if absent — it also auto-adds a
script to
). The rule is simply:
do not remove it. Optionally add
(root-level array of wallet addresses) to authorize reading production server logs — see
{baseDir}/references/server-patterns.md
→ Production Logs. The preview automatically starts a local server in the background.
Server/Client Branching
Use
from
to branch logic in a single codebase. Server runs headlessly (no rendering) and has access to all player positions via
.
For
shared/library code that resolves the role itself via the low-level
from
(async, unlike the sync
helper), use the defensive idiom: resolve the role once at startup, have systems return early while it is still unknown, keep client-only features permanently off on the server, and treat a
failed query as
client — so a real client never loses functionality if the query errors.
Synced Components with Validation
Define custom components that sync from server to all clients.
Always use
to prevent clients from modifying server-authoritative state.
Always guard (and any helper that wraps it, like ) inside an block — both overloads (per-entity and global no-entity) only have meaning on the server, and calling either on a client produces errors. This applies even to global custom-component validators in shared files: define the component at module scope, but place the
call inside an
guard (e.g. inside
or inside an
block in
).
The validator callback receives
{ entity, currentValue, newValue, senderAddress, createdBy }
. Read component fields from
(NOT
— that field does not exist).
is the pre-change value (
if component was not present).
is
when the component is being deleted.
is the wallet address of the sender; equals
when sent by the server. Always compare addresses with
.
Validation Patterns
- Pattern 1 — Server-only writes (strictest):
Score.validateBeforeChange((v) => v.senderAddress === AUTH_SERVER_PEER_ID)
- Pattern 2 — Validate the value itself: reject impossible values (e.g.
value.newValue.position.y > 0
)
- Pattern 3 — Proximity validation (anti-cheat): check player is near the object via + . Server-read player is scene-local metres — the same frame the client sees and the same frame scene entities use — so compare it directly to entity positions with , no base-parcel offset math. Canonical example: the official
90,-9-authoritative-server-leaderboard
test scene (deployed at non-origin parcels) compares server-read player position directly to a scene-local target. (Some older SDK server builds returned world/parcel-absolute coordinates instead; that was a bug, since fixed — do not add offset corrections.)
- Pattern 4 — Admin-only writes: use from
@dcl/asset-packs/dist/admin-toolkit-ui/ModerationControl/api
to restrict to admins. For a lightweight fixed allow-list, gate the message handler on the server-verified sender instead: keep a lower-cased in shared config and check ADMINS.includes(context.from.toLowerCase())
inside (never trust a client-reported role). Clients may read the same list to decide whether to show a privileged button, but only the server's check is authoritative.
Use
from
@dcl/asset-packs/dist/admin-toolkit-ui/fetch-utils
(sync, no args, returns
) to relax validation during local development. The deep
import path is the only working one — the package has no top-level re-export.
Custom components use global validation:
GameState.validateBeforeChange((value) => ...)
.
Built-in components (Transform, GltfContainer) use per-entity validation:
Transform.validateBeforeChange(entity, (value) => ...)
.
After creating and protecting an entity, sync it with
syncEntity(entity, [Transform.componentId, GameState.componentId])
.
In an authoritative-server scene, only the server should call — wrap the call in
. The server creates and shares the entity instance; all clients receive the sync. This is different from the
pattern (serverless), where every client calls
on its own. Calling
on the client in an authoritative scene produces errors, and avoiding client-side calls also removes the need to worry about entity-id consistency across peers.
Per-Player Synced Entities
For server-created entities that exist one-per-player (score, hold time, wallet),
never derive an explicit sync id from the player's address (e.g.
). An explicit sync id is a
global network identity with a hard collision check — a hashed id throws
syncEntity failed because the id provided is already in use
both when two addresses hash into the same slot (~50% odds by ~370 players for a 100k range) and when the same player reconnects before their old entity is cleaned up. Instead
omit the id (
syncEntity(entity, [Comp.componentId])
) — auto-allocation is unique by construction — and store the player's address in a component field (
); all readers (client systems and server-restart reconciliation) match on that field, never on the network id. Reserve explicit enum ids for fixed singletons (game state, flag, leaderboard).
Long-running servers recycle entity slots, so cached
handles can go stale (component gone while the map still points at the dead entity). Validate the cache with
before reuse and recreate on a stale hit; in per-frame systems use
+ guard so a transient miss skips one tick instead of throwing
[mutable] Component <name> for <id> not found
every frame. When adopting entities from an
engine.getEntitiesWith(Comp)
scan (e.g. rebuilding the map after a server restart), skip any entity whose number (
) is below 512 — those are reserved/avatar-range slots owned by the runtime; never cache or
them. Full pattern with code:
{baseDir}/references/server-patterns.md
→ Per-Player Synced Entities.
Messages
Use
for client-to-server and server-to-client communication. Define message schemas with
— plain JS objects will fail binary serialization.
Module-load timing (critical): defines a component internally, and
in
defines components too. Both MUST run during initial module load, before the engine seals. Reach them via
static (e.g.
import './shared/messages'
at the top of
), NOT via a dynamic
inside
— a dynamic import runs after the seal and throws
. Only server-only modules (those importing
) should be dynamically imported inside the
branch, and only if they define no components at module scope — this keeps
out of the client bundle path.
- Client sends:
room.send('playerJoin', { displayName: 'Alice' })
- Server sends to all:
room.send('gameEvent', { ... })
- Server sends to one:
room.send('gameEvent', { ... }, { to: [playerAddress] })
- Receive:
room.onMessage('playerJoin', (data, context) => { ... })
— is the sender's wallet
Clients must wait for
(note SDK typo) to return
before sending messages.
IMPORTANT — message size limit: Never send messages larger than 13 KB. The transport will silently drop any message that exceeds this limit. Split large payloads into smaller chunks if needed.
is client-only. (from
) subscribes to the legacy
EngineApi.subscribe('comms')
event, which the headless server runtime does not implement — on the server it fails with
RemoteError: not implemented
. Because module-scope code runs on both sides, never construct one at module scope (
const bus = new MessageBus()
) in an authoritative-server scene; construct it only inside the client branch (
). It remains fine client-side for transient client-to-client effects, but the server can neither send nor receive on it — all client↔server communication must go through
+
.
Schema Types Reference
,
,
,
,
(for
/ 13+ digit numbers),
,
,
,
,
.Optional(Schemas.String)
,
.Map({ name: Schemas.String, hp: Schemas.Int })
.
The boolean schema is
, not
(verified —
exposes
; the internal class is named
but is not exposed under that name on the
namespace).
Use for timestamps —
corrupts large numbers (13+ digits).
Server Reading Player Positions
Read actual server-verified positions via
engine.getEntitiesWith(PlayerIdentityData)
+
Transform.getOrNull(entity)
. Never trust client-reported positions.
Storage
Persist data across server restarts.
Server-only — guard with
. Import from
.
- Scene Storage (global, shared across all players):
Storage.set/get/delete(key)
— top-level methods on
- Player Storage (per-player, scoped by wallet address):
Storage.player.set/get/delete(address, key)
Storage only accepts strings — use
/
for objects. Local dev storage is at
node_modules/@dcl/sdk-commands/.runtime-data/server-storage.json
. Production storage at
decentraland.org/storage. CLI:
npx sdk-commands storage scene/player set/get/delete ...
. Storage persists across deploys (scoped to world, not hash).
IMPORTANT — storage writes are capped, do NOT write on every change/tick: A scene that fires a
per score change / per event / per tick hits the isolate's shared in-flight host-call cap; the excess write fails
silently — the SDK resolves it to
rather than throwing.
/ return — check it (
= the write did not persist; retry or surface it). Keep live/working state
in memory (faster and correct for a server) and persist to Storage only at meaningful checkpoints: game over, player leaves, or a periodic debounced save. Persist only data that must survive server restarts/deploys. For the cap mechanism and the checkpoint pattern, see
{baseDir}/references/server-patterns.md
→ Storage Patterns and Server Resource Limits.
Live storage web UI (
decentraland.org/storage, also reachable from Creator Hub
Manage → three dots next to a published place →
View Storage). Three tabs —
Scene,
Player,
Environment. Edits apply to the running scene
live, without republishing:
- Scene tab: view/edit/delete the shared variables (leaderboard, door state). Handy for tweaking live values, e.g. resetting a leaderboard.
- Player tab: look up a player by wallet address or name and inspect/edit/clear their stored data. Main use is support — un-wedge a player stuck in a bad state (e.g. contradictory data from an older scene version) without redeploying.
Environment Variables
Configure values without hardcoding.
Server-only.
EnvVar.get(key: string): Promise<string>
from
— always resolves to a string, returns
(empty string) when the variable isn't set or the fetch fails (never
). The
pattern still works for defaults since
evaluates to
. Use
file locally (add to
). Deploy with
npx sdk-commands storage env set KEY --value VALUE
. Production UI at
decentraland.org/storage →
Environment tab (or Creator Hub → Manage → three dots →
View Storage).
- Right place for secrets (private keys, reward/claim codes, API keys) — the values only ever exist on the server, never reach the client or the published scene code.
- Write-only in the UI: you can add, overwrite, or delete a variable, but you cannot read the current value back (intentional, to protect secrets).
- Also ideal for live-tunable game parameters / feature flags (match duration, max player count) you want to adjust on the running scene without republishing.
Recommended Project Structure
src/
├── index.ts # Entry point — isServer() branching
├── client/
│ ├── setup.ts # Client init, input handlers, message senders
│ └── ui.tsx # React ECS UI (reads synced state, sends messages)
├── server/
│ ├── server.ts # Server init, systems, message handlers
│ └── gameState.ts # Server state management class
└── shared/
├── schemas.ts # Synced component definitions + validateBeforeChange
└── messages.ts # Message definitions via registerMessages()
Performance Best Practices
Every component change sends the entire component data. Prefer atomic components over monolithic ones — group fields that change together, separate fast-changing data from slow-changing data. Throttle frequent messages (never send every frame). For derivable state, broadcast every ~30s and compute locally between.
Server Lifecycle
The server is only active while at least one player is in the scene. After the last player leaves it stays up for roughly two minutes, then shuts down. The next visit cold-starts a fresh instance, which takes ~15 seconds in production. Local preview launches the server instantly — which is exactly why server-readiness bugs almost always escape into production unnoticed. Always test the "no players have been here for a while" path against a real deploy.
is not a server-readiness check. It only confirms the CRDT room transport is connected. The room's CRDT snapshot can hold state persisted from a
previous server run, so a fresh client may see "valid" state while the server is still booting — or while it never wakes up at all because this client is the only one and the platform hasn't started one yet. Messages sent in that window are silently lost and the scene wedges waiting for a server response that will never come.
The reliable pattern is a
server heartbeat: the server writes
to a synced component field every ~2 s; the client tracks the
client-side time at which it observed the value last change (not the server's timestamp) and treats the server as alive only if a tick has been observed within ~3× the interval. Tracking client-observed time, not the heartbeat value, means a stale snapshot from a long-gone server run does not read as live, and clock skew between server and client is irrelevant. Publish the first heartbeat
inside the server's state-init function so the first client to connect doesn't have to wait a full interval.
Distinguish two failure modes at the UI layer — they look similar but behave very differently. Room-not-synced is transient (~1 s during scene load): buffer the action and auto-fire it from a retry system. Server-not-alive can last 15 s or more on a cold start and may never resolve: surface a "server waking up" popup rather than silently buffering, and auto-dismiss it the moment a heartbeat lands so a player who waited isn't left staring at a stale dialog. See
{baseDir}/references/auth-server-examples.md
→ Server Liveness Heartbeat for a full implementation.
Version Control of Deploys
Client and server always move together (paired by hash). Existing players keep the old version until they rejoin.
data persists across versions.
Testing & Debugging
- Log prefixes: Use and in
- Local multi-player: Click Preview a second time in Creator Hub, or open
decentraland://realm=http://127.0.0.1:8000&local-scene=true&debug=true
- Production logs:
npx sdk-commands sdk-server-logs
(add --world WORLD_NAME.dcl.eth
for Worlds). Prompts a wallet-signature challenge; signing wallet must be listed in . See {baseDir}/references/server-patterns.md
→ Production Logs.
- Server-log noise signatures: two recurring errors in server logs both mean client-only code is running in the server branch.
RemoteError: not implemented
on EngineApi.subscribe('comms')
→ a was constructed on the server (see Messages). 400 Invalid metadata content
from comms-gatekeeper.decentraland.org
→ server code called a client-context platform API via . Neither crashes the server, but the repeating noise buries real server logs — gate the offending code behind .
- Per-player entity error signatures:
syncEntity failed because the id provided is already in use
→ an explicit sync id collided (usually a hash-derived per-player id, or a same-frame remove-and-recreate with the same id); [mutable] Component <name> for <id> not found
repeating every frame → a cached entity handle went stale on a long-running server. Both fixes in Per-Player Synced Entities above.
- Stale CRDT files: Delete and and restart
- Storage inspection: Check local JSON file or decentraland.org/storage
- Timers & sandbox: QuickJS sandbox — no Node.js APIs (, , etc.). Use / from for delays — never the native JS globals. Prefer with dt accumulator for game logic
- Entity sync: Verify
syncEntity(entity, [componentIds])
with correct values
Example scenes
-
https://github.com/decentraland/sdk7-test-scenes/tree/main/scenes/90,-9-authoritative-server-leaderboard — full end-to-end authoritative leaderboard. Clients send a
action (never a score); the headless server validates proximity to a "score orb" (scene-local coordinates, compared directly), increments the score itself, persists per-player totals to
, and broadcasts a synced top-N
component that all clients render. Shows:
authoritativeMultiplayer: true
in
,
branching, static-import of
/
for module-load timing, custom-component
gated by
(server-only writes via
), server-only
, atomic components (heartbeat kept separate from the board), and the server-liveness heartbeat with client-observed-time tracking.
-
https://github.com/decentraland/sdk7-test-scenes/tree/main/scenes/92,-9-authoritative-server-gem-rush — full competitive game with anti-cheat + checkpoint persistence. Key patterns: server-side proximity validation via
+
(never trust client positions, server reads player transform directly and compares with
); position stored in a guarded custom component (not synced Transform) to prevent client write-back; atomic component split by change rate (countdown separate from scoreboard separate from hall of fame); Storage writes ONLY at round end (1 scene SET + N player SETs per round, 0 calls during gameplay);
flag in rejection messages;
with
pulsed on boot + every ~2 s; per-player stats lazy-loaded from
once per session and cached in memory.
-
https://github.com/decentraland/sdk7-test-scenes/tree/main/scenes/93,-9-authoritative-server-limits-lab — stress-tests every runtime limit:
40,
32,
512,
30000,
300/s,
100000,
256 MB, sync 10 s / async 60 s, fetch timeout 15 s, max WS message 1 MB, max open sockets 32, max fetch redirects 5, max body 10 MB. Reference for the limit values in
{baseDir}/references/server-patterns.md
→ Server Resource Limits.
For full code examples (validation patterns, messages, Storage, EnvVar, performance), see
{baseDir}/references/auth-server-examples.md
. For server setup patterns, see
{baseDir}/references/server-patterns.md
.