How cbournhonesque/lightyear works
1. What this repository is
- Lightyear is a Rust workspace for building server-authoritative multiplayer games on Bevy.
- The repo is split into reusable crates rather than a single application, so the architecture is layered: low-level transport at the bottom, networking/session layers above that, then replication/input/sync features, then Bevy integrations and examples on top.
2. Primary runtime model
- The core runtime is a Bevy plugin-based ECS system.
- Games typically run as a client, server, or shared Bevy app, with plugins wiring network events into ECS resources, components, and systems.
- Most subsystems are designed to operate on ticks/timelines so that simulation, inputs, replication, and prediction stay aligned.
3. Transport and packet layer
- crates/transport/transport is the packet/channel engine: it handles packet framing, compression, priorities, fragmentation, acknowledgements, and reliability modes.
- crates/transport/messages defines the typed message API that sits on top of packet transport.
- crates/transport/serde provides the serialization layer used to encode messages and entity references.
- This layer is the boundary between game/network semantics and raw bytes.
4. Connection and session establishment
- crates/connection contains abstractions for client/host/server identities, connection roles, targets, and shared networking state.
- Subcrates implement specific connection strategies:
- netcode for authenticated secure client/server sessions
- raw_connection for simpler direct connections
- steam for SteamWorks-based networking
- These crates define how peers authenticate, identify each other, and map logical peers onto transport endpoints.
5. I/O backends and network plumbing
- crates/io contains the actual transport backends:
- udp for native socket networking
- websocket and webtransport for browser-compatible networking
- aeronet and crossbeam for other runtime/link-style backends
- link for local/test network conditioning and simulated link behavior
- These backends are the lowest boundary before packets leave the process, and they are what make the stack transport-agnostic.
6. Core time, sync, and simulation alignment
- crates/core/core provides shared primitives like tick handling, time tracking, interpolation, prediction history, and timeline management.
- crates/core/sync handles client/server time synchronization and ping estimation, including timeline syncing and diagnostics.
- crates/core/frame_interpolation is a small isolated crate for frame interpolation logic.
- These crates make sure networked state changes are interpreted against the right simulation time.
7. Replication and world state flow
- crates/replication/replication is the main entity/component replication engine.
- It handles authority, delta/state updates, checkpoints, send/receive paths, hierarchy handling, prespawning, visibility/interest management, and replication registry logic.
- Data flow here is typically: ECS state is inspected on the authoritative side → encoded into network messages → transmitted through transport → decoded and applied on remote worlds.
- This is the central subsystem for world synchronization.
8. Prediction and interpolation
- crates/replication/prediction implements client-side prediction with rollback/correction.
- crates/replication/interpolation handles snapshot interpolation for non-controlled or remote entities.
- Together they split remote-state handling into two paths:
- predicted entities advance locally and reconcile later
- interpolated entities lag behind slightly and smooth over packet cadence
- These crates are key to the “responsive client / authoritative server” architecture shown in the README.
9. Input networking
- crates/inputs contains the generic input pipeline, including buffering and network message types.
- inputs_native, input_bei, and inputs_leafwing are adapter crates for different input sources, including Bevy Enhanced Input and Leafwing Input Manager.
- Input flow is usually: local device input → buffered per tick on client → sent with redundancy → applied on the server for the matching tick.
- This is a distinct path from world replication because player intent must be time-aligned, loss-tolerant, and often replayable.
10. Deterministic replication mode
- crates/deterministic/deterministic_replication supports input-only deterministic simulation.
- It includes checksums, late-join logic, and mode control to support lockstep-style or deterministic rollback-style networking.
- This subsystem is separate from ordinary state replication because it assumes the simulation itself can be reproduced exactly from the same inputs.
11. Integration layer for game physics and ECS ecosystems
- crates/integration/avian and related crates provide Avian physics integration and lag compensation helpers.
- This is where network sync meets gameplay systems like rigid bodies, collision queries, and predicted/interpolated physics objects.
- The integrations act as adaptation layers between Lightyear’s network model and specific Bevy ecosystem crates.
12. Platform and browser support
- crates/platform/web contains browser-oriented support, including a background worker utility.
- Combined with websocket and webtransport, this is what enables the same networking architecture to run in wasm/browser environments as well as native ones.
13. Tooling, observability, and developer support
- crates/tools/metrics and crates/tools/tools provide diagnostics, debug UI, schema/component inspection, and tracing support.
- The repo also uses tracing and metrics as first-class observability hooks across the networking stack.
- This tooling is not the data path itself, but it matters architecturally because it exposes transport, replication, and sync behavior at runtime.
14. Examples, tests, docs, and validation
- examples/ and demos/ are the main architectural usage references: each example composes a client, server, shared protocol, and renderer around specific Lightyear features.
- crates/tests is a dedicated integration-test workspace covering client/server, host-server, multi-server, timeline sync, prediction, input, and replication scenarios.
- book/ is the conceptual documentation for the architecture, while benches/ measures compile-time and runtime performance of transport, replication, and compression paths.
15. How the major data flows connect
- The most important diagram boundary is:
- Bevy gameplay systems and ECS state
- through input/replication/prediction/sync plugins
- into message serialization and packet channels
- across connection + IO backends
- to the remote client/server world
- The architecture is intentionally modular so that a project can use only the transport layer, only replication, or the full authoritative client/server stack.
Main entry points: examples Examples · transport Transport · connection Connection · inputs Inputs
Components 30
Transport
Packet framing, typed messages, and serialization boundary.
- Transport · Reliable/unreliable channels, packet building, fragmentation, compression, and acks.
crates/transport/transport - Messages · Typed send/receive abstractions built on top of packet transport.
crates/transport/messages - Serde · Encoding and decoding for messages, entity references, and registry-backed formats.
crates/transport/serde
Conn + I/O
Peer identity, session setup, and transport backends.
- Connection · Roles, targets, identities, and shared networking state.
crates/connection/connection - Netcode · Authenticated client/server connection flow with tokens, crypto, and replay protection.
crates/connection/netcode - Raw Conn · Simpler direct connection strategy without secure netcode handshake.
crates/connection/raw_connection - Steam · SteamWorks-based client/server connection strategy.
crates/connection/steam - UDP · Datagram backend for native networking.
crates/io/udp - WebSocket · Web-compatible client/server transport backend.
crates/io/websocket - WebTransport · Modern browser-friendly transport backend.
crates/io/webtransport - Link · Local/test network conditioning and link-style transport behavior.
crates/io/link
Core time
Shared tick, time, sync, and utility primitives.
- Core · Ticks, time tracking, timelines, interpolation history, and prediction support.
crates/core/core - Sync · Ping estimation, timeline synchronization, and diagnostics.
crates/core/sync - Frame Interp · Isolated frame interpolation helpers.
crates/core/frame_interpolation
Replication
World-state replication, prediction, and interpolation.
- Replication · Authority, delta/state updates, checkpoints, send/receive, hierarchy, visibility, and registry logic.
crates/replication/replication - Prediction · Client-side prediction, rollback, correction, and predicted history.
crates/replication/prediction - Interpolation · Snapshot interpolation for remote or non-controlled entities.
crates/replication/interpolation - Deterministic · Deterministic replication mode with checksums, late join, and mode control.
crates/deterministic/deterministic_replication
Inputs
Buffered input pipelines and source adapters.
Integration
Physics and ECS ecosystem adapters.
- Avian · Avian physics sync, correction, and lag compensation helpers.
crates/integration/avian - Web Platform · Browser-oriented runtime support including background worker utilities.
crates/platform/web
Tools
Observability and debug support.
- Metrics · Metrics plugin and registry support.
crates/tools/metrics - Debug Tools · Debug UI, schema/component inspection, and tracing layers.
crates/tools/tools
Apps
Examples, demos, tests, and documentation consumers.
- Examples · Client/server/shared example apps demonstrating Lightyear subsystems.
examples - Demos · Larger showcase applications built from the same crates.
demos - Tests · Scenario-driven client/server, host/server, timeline, input, prediction, and replication tests.
crates/tests - Book · Conceptual documentation for transport, connection, replication, inputs, and integration.
book
Connections 44
- Messages → Transport: builds on · Typed messages are carried by packet/channel transport.
- Serde → Messages: encodes · Serialization feeds message and entity wire formats.
- Connection → Transport: uses · Connection state rides on top of packet transport.
- Netcode → Connection: extends · Secure handshake and session logic builds on shared connection roles.
- Raw Conn → Connection: extends · Direct session strategy builds on shared connection abstractions.
- Steam → Connection: extends · Steam networking maps into the shared connection model.
- UDP → Transport: feeds bytes · Native UDP backend is the process boundary for packets.
- WebSocket → Transport: feeds bytes · WebSocket backend carries packets in browser-compatible environments.
- WebTransport → Transport: feeds bytes · WebTransport backend carries packets in browser-compatible environments.
- Link → Transport: feeds bytes · Simulated link backend injects conditioning and local transport behavior.
- Sync → Core: depends on · Time sync uses the shared tick/timeline primitives.
- Replication → Core: aligns to · Replication is tick and timeline aware.
- Prediction → Core: aligns to · Prediction uses confirmed history, rollback, and timelines.
- Interpolation → Core: aligns to · Interpolation advances snapshots against shared time primitives.
- Deterministic → Core: aligns to · Deterministic mode uses tick-aligned simulation state.
- Inputs → Core: aligns to · Input buffering is organized per tick and timeline.
- Native → Inputs: adapts · Native input sources feed the generic input pipeline.
- BEI → Inputs: adapts · Bevy Enhanced Input feeds the generic input pipeline.
- Leafwing → Inputs: adapts · Leafwing Input Manager feeds the generic input pipeline.
- Avian → Prediction: integrates with · Physics correction and lag compensation connect to prediction.
- Avian → Interpolation: integrates with · Physics state can be smoothed through interpolation.
- Web Platform → WebSocket: supports · Web platform utilities support browser transport runtimes.
- Web Platform → WebTransport: supports · Web platform utilities support browser transport runtimes.
- Metrics → Sync: observes · Metrics capture sync and timing behavior.
- Debug Tools → Transport: inspects · Debug tooling exposes transport and packet state.
- Debug Tools → Replication: inspects · Debug tooling exposes replication state and metadata.
- Debug Tools → Sync: inspects · Debug tooling surfaces sync diagnostics.
- Examples → Connection: composes · Examples wire client/server/shared apps around connection setup.
- Examples → Inputs: composes · Examples demonstrate input buffering and adapters.
- Examples → Replication: composes · Examples demonstrate entity replication flows.
- Examples → Prediction: composes · Examples demonstrate prediction and correction flows.
- Examples → Interpolation: composes · Examples demonstrate interpolation of remote entities.
- Examples → Avian: composes · Examples show physics integration with networking.
- Demos → Examples: extends · Demos are higher-level showcase applications built from the same stack.
- Tests → Connection: verifies · Integration tests cover connection modes and topologies.
- Tests → Sync: verifies · Tests cover timeline and sync behavior.
- Tests → Replication: verifies · Tests cover replication, hierarchy, visibility, and authority.
- Tests → Prediction: verifies · Tests cover prediction, rollback, and correction.
- Tests → Inputs: verifies · Tests cover native and adapter input paths.
- Book → Transport: documents · Book explains the transport and serialization model.
- Book → Connection: documents · Book explains connection strategies and roles.
- Book → Replication: documents · Book explains replication protocol and state flow.
- Book → Inputs: documents · Book explains networked input handling.
- Book → Avian: documents · Book explains integration patterns and advanced systems.