Networking

State Sync

How entity state is serialized and broadcast from server to clients.

State synchronization is the process by which the server broadcasts the authoritative world state to connected clients on each tick.

How it works

  1. Server tick — On each fixed tick (default 30/s), the server steps the physics world and updates all entities
  2. Serialization — Each entity's state is serialized via serialize() (from GameObject)
  3. Distance filtering — Only entities within getStateSyncDistance() of a given client are included in that client's state packet
  4. Broadcast — The filtered state is sent to each client over the WebRTC socket
  5. Client apply — The client calls updateFromNetwork(data) on each matching local entity, and creates unknown ones with the NetworkedEntityFactory

What is synced

  • Scene objects are map data every client loads itself, so they are not sent, unless they are networked entities (NetworkedGameObject).
  • Clients running the level editor are no players: they get every synced entity in the world, regardless of distance.
  • When a player disconnects, the server flags it with garbageCollect; the world destroys it on the next tick, and nearby clients get SV_REMOVE_ENTITY.

Client-side prediction

Clients apply their own inputs right away and send them with NetworkManager.sendCommand(), which gives each one a sequence id. The server applies them in order and sends each player's lastProcessedSequenceId back in the state. The client drops the acknowledged inputs with dropCommandsAtSequenceId() and corrects its player from the server's state. See NetworkManager.

Distance-based visibility

The server only sends state for entities within a configurable radius of each connected client. This reduces bandwidth for large worlds.

class MyServer extends Server {
  getStateSyncDistance(): number {
    return 150 // Only sync entities within 150 units
  }
}

Bandwidth tracking

Monitor sent and received bandwidth with the built-in tracker:

server.bandwidthTracker // Access bandwidth stats

The dashboard shows it live, per second.

State packet format

Every SV_STATE is stamped by the server, so clients and tools can tell states apart:

FieldDescription
tickNumber of the server tick the state was taken in. Goes up by one per tick, so a gap means a lost packet
serverTimeEpoch ms on the server when the state was taken
ackSequence id of the receiver's last input the server processed before this state. null for a client without a player, like the editor
entitiesEach entity's serialize() output: id, position, rotation, state hash and any custom fields

The client keeps the stamps of the last state in NetworkManager.serverView.

Entities with garbageCollect === true are omitted from state packets and the client is notified to remove them.

Network debugging

In development both sides record their traffic: every message through the game socket, the engine's and your own, with its size, and events such as:

EventSide
connect / disconnectboth
droppedbothThe transport dropped a message
sync_enter / sync_leaveserverEntities coming into or leaving a player's sync distance
state_gapclientState updates that never arrived
state_out_of_orderclientA state that arrived after a newer one

Each side keeps its last 2000 records, and 8 MB of their payloads. The server records when NODE_ENV is not production (server.net); a tab records while the dev bridge or the editor runs (network.net, or turn it on with network.net.enabled = true). Nothing is recorded, and the socket is left untouched, in a production build.

server.syncSnapshot() tells who is sent what: each player's tracked entities and last state update, and by how many players each entity is tracked.

Look at it in:

  • the Network tab of the dashboard: state sync per player, traffic by message and every packet with its payload;
  • the Network panel of the editor: packet list, timeline, state sync, and server ghosts and sync radius rings in the viewport;
  • the network tools of the MCP server, for an AI assistant.

Tick rate

The server tick rate controls how frequently state is sent. Default is 30 ticks/second, matching common game server conventions (sv_tickrate).

const game = BaseGame.instance()
game.tickRate // 30 (default)

To test how your game feels on a bad connection, enable the server's latency simulation.

A higher tick rate gives smoother interpolation but increases bandwidth and CPU usage.