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
- Server tick — On each fixed tick (default 30/s), the server steps the physics world and updates all entities
- Serialization — Each entity's state is serialized via
serialize()(fromGameObject) - Distance filtering — Only entities within
getStateSyncDistance()of a given client are included in that client's state packet - Broadcast — The filtered state is sent to each client over the WebRTC socket
- Client apply — The client calls
updateFromNetwork(data)on each matching local entity
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 outgoing bandwidth with the built-in tracker:
server.bandwidthTracker // Access bandwidth stats
State packet format
Each entity contributes its serialize() output to the state packet. The format is:
- Entity ID (key)
- Serialized state (value) — position, rotation, state hash, and any custom fields
Entities with garbageCollect === true are omitted from state packets and the client is notified to remove them.
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)
A higher tick rate gives smoother interpolation but increases bandwidth and CPU usage.