Skip to main content

Crate naia_client

Crate naia_client 

Source
Expand description

Client-side half of the naia real-time entity replication and messaging library.

The central type is Client<E>, which connects to a naia server, maintains a local mirror of replicated entities, and sends client messages and (optionally) client-authoritative entity mutations.

Runs natively (UDP) and in the browser (WebRTC via wasm32-unknown-unknown). The same application logic compiles for both targets; only the Socket implementation differs.

§Connection setup

// Optional: include an auth message in the handshake.
// client.auth(MyAuthMessage { token: "…".into() });

client.connect(socket); // begin handshake; loop until ConnectionEvent fires

§Main loop

loop {
    client.receive_all_packets();                     // 1. read from socket
    client.process_all_packets(world, &now);          // 2. decode + apply
    let events: Events<E> = client.take_world_events(); // 3. drain events
    let ticks: TickEvents = client.take_tick_events(&now); // 4. tick clock
    // 5. apply prediction / interpolation here
    client.send_all_packets(world);                   // 6. flush outbound
}

§Key types

TypePurpose
Client<E>Main entry point
EntityMutBuilder for client-authoritative entities
EntityRefRead-only entity handle
PublicityThe three visibility states (Private / Public / Delegated)
CommandHistoryRollback buffer for client-prediction
ConnectionStatusLifecycle state

Modules§

counters
Internal e2e-debug atomic counters used by the test harness.
shared
Re-exports of commonly-used naia_shared types, scoped for client consumers.
transport
Transport-layer socket abstractions (UDP, WebRTC, local in-process).

Structs§

Client
The naia client — connects to a server, receives replicated entities, and sends client-authoritative mutations and messages.
ClientConfig
Contains Config properties which will be used by a Server or Client
ClientTickEvent
Fired each client-side simulation tick; iterate via tick_events.read::<ClientTickEvent>().
CommandHistory
Ring buffer of (tick, command) pairs for client-prediction rollback; old entries are pruned when the server acknowledges a tick.
ConnectEvent
Fires when the client successfully establishes a connection to the server; yields the server’s SocketAddr.
ConnectionStats
Snapshot of per-connection network diagnostics.
DespawnEntityEvent
Fires when the server despawns a previously replicated entity; yields the world entity E.
DisconnectEvent
Fires when the connection to the server is lost; yields the server address and the DisconnectReason.
EntityAuthDeniedEvent
Fires when the server denies this client’s authority request for a delegated entity.
EntityAuthGrantedEvent
Fires when the server grants this client authority over a delegated entity.
EntityAuthResetEvent
Fires when the server reclaims authority over an entity that was previously delegated to this client.
EntityMut
Scoped mutable handle for a client-owned entity.
EntityPriorityMut
Mutable handle for reading and setting an entity’s priority in one priority layer. Lazy-creates a state entry on first write so set-and-forget works even before the entity enters scope for that user.
EntityPriorityRef
Read-only view of an entity’s priority state in one priority layer (global OR per-user). Acquired via the corresponding *_priority() method on WorldServer, Client, or their Bevy-adapter equivalents.
EntityRef
Scoped read-only handle for a client entity.
ErrorEvent
Fires when a transport or protocol error occurs; yields a NaiaClientError.
Events
All events produced in one frame: connections, entity lifecycle, component changes, messages, and errors.
InsertComponentEvent
Fires when component C is inserted on a replicated entity; yields the world entity E.
MessageEvent
Fires when a message of type M arrives on channel C; yields the decoded M value.
PublishEntityEvent
Fires when an entity transitions to the Public visibility state and becomes visible to all users.
RejectEvent
Fires when the server explicitly rejects the connection; yields the server address and the RejectReason.
RemoveComponentEvent
Fires when component C is removed from a replicated entity; yields (E, C) with the last value of the component.
RequestEvent
Fires when a request of type Q arrives on channel C; yields a (ResponseSendKey, Q) pair.
ServerTickEvent
Fired each time the server’s authoritative tick advances; iterate via tick_events.read::<ServerTickEvent>().
SpawnEntityEvent
Fires when the server spawns a new replicated entity on this client; yields the world entity E.
TickEvents
Collects client and server tick events emitted during a single frame for typed iteration.
UnpublishEntityEvent
Fires when an entity’s visibility is retracted from the Public state.
UpdateComponentEvent
Fires when component C on a replicated entity is mutated; yields (Tick, E).

Enums§

ConnectionStatus
The lifecycle state of the client’s connection to the server.
DisconnectReason
Why a connection was terminated.
EntityOwner
The authority origin of a client-tracked entity.
JitterBufferType
Configuration type for jitter buffer behavior
NaiaClientError
Errors that can be returned by the naia client.
Publicity
Publication visibility of an entity — the shared axis used on both sides of the connection.

Traits§

TickEvent
Type-indexed tick event; implemented by ClientTickEvent and ServerTickEvent.
WorldEvent
Type-indexed world event; each concrete type selects one category from Events.