Diese Seite ist noch nicht auf Deutsch übersetzt und wird daher auf Englisch angezeigt.
Multiplayer & Moddingnetplay
game/online/netplay provides cross-target multiplayer networking for NSGo games: a unified Transport across WebTransport (HTTP/3 QUIC, native + WebAssembly), WebSocket (native + WebAssembly), native UDP, and simulated loopback; server-authoritative delta snapshot replication with client prediction and interpolation; and deterministic lockstep and rollback netcode.
import "github.com/nsgo-dev/game/pkg/online/netplay"
Cross-target transports (Transport)
Every transport implements the same non-blocking Transport interface (Send, Recv, LocalID, Peers, Close) with ChannelReliableOrdered and ChannelUnreliable channels and PeerBroadcast (PeerID(0)):
| Transport | Constructor | Targets & use case |
|---|---|---|
WebTransportServer / WebTransportTransport |
ListenWebTransport, ListenWebTransportFiles, DialWebTransport |
Native + browser (js/wasm); sends ChannelUnreliable as QUIC datagrams (no TCP head-of-line blocking) and ChannelReliableOrdered over one bidirectional QUIC stream, encrypted with TLS 1.3. |
WebSocketServer / WebSocketTransport |
ListenWebSocket, ListenWebSocketTLS, DialWebSocket |
Native + browser (js/wasm); works across NATs, TCP-only networks, and older browsers. Both channels share one ordered TCP stream. Clients authenticate with a token and the server assigns their IDs (see below). |
UDPServer / UDPClient |
ListenUDPServer, DialUDP |
Native targets (desktop/mobile) for low-latency encrypted datagrams to a server, using an address-checking cookie round trip, a Noise NK key exchange (handshake version 3) and ChaCha20-Poly1305 AEAD. |
UDPTransport |
ListenUDP + AddPeerKey / AddPeer |
Native peer-to-peer datagrams between endpoints that know each other's addresses: AddPeerKey authenticates and encrypts with a pre-shared key; plain AddPeer is unauthenticated for LAN use and tests. |
Relay / RelayTransport |
NewRelay, NewRelayTransport |
Peer-to-peer sessions between players who cannot reach each other: each connects to a relay over WebTransport, WebSocket, or UDP, and the relay forwards between members of the same session (see the server guide). |
LoopbackHub / LoopbackTransport |
NewLoopbackHub + AddPeer |
In-memory deterministic tests with configurable Latency, Jitter, LossRate, and Seed. |
UDP has no retransmission or ordering, so both UDP transports refuse ChannelReliableOrdered with ErrUnsupportedChannel; lockstep and rollback need a WebTransport, WebSocket, or loopback transport, or a RelayTransport, which retransmits end to end and so runs them over UDP too.
DialWebSocket speaks netplay's own handshake and framing, so it connects only to a WebSocketServer. To talk to a WebSocket server that is not part of this repository — a third-party backend, a chat service, a custom protocol — use wsclient instead.
A transport has a single consumer. Recv hands each packet to one caller, and ServerWorld, ClientWorld, the lockstep and rollback sessions, Relay and RelayTransport all drain every waiting packet and discard the ones that are not theirs, so two of them on one transport steal each other's packets. Give each consumer its own transport, or put a demultiplexer in front of them that implements Transport for each. Recv costs the same however many packets are queued, so draining a burst stays linear.
If the socket under a UDPTransport or UDPServer fails, Err returns ErrReceiveFailed wrapping the socket's error and Send returns it too; close the transport and listen again; after Close, Err returns nil. Errors about a single datagram, such as an ICMP port unreachable for an earlier send, do not stop it. A UDPClient likewise keeps reading after an ICMP error, for example while its server restarts, and leaves it to its idle timeout to decide whether the server is gone.
hub := netplay.NewLoopbackHub(netplay.LoopbackConfig{
Latency: 40 * time.Millisecond,
Jitter: 10 * time.Millisecond,
LossRate: 0.02,
Seed: 7,
})
serverTx := hub.AddPeer(1)
clientTx := hub.AddPeer(2)
_ = serverTx.Send(2, netplay.ChannelReliableOrdered, []byte("hello"))
hub.Advance(60 * time.Millisecond)
pkt, ok := clientTx.Recv()
_, _ = pkt, ok
WebTransport (unreliable browser transport)
Over WebSocket, ChannelUnreliable still rides on TCP, so a single lost packet stalls every newer snapshot or rollback input behind it until TCP retransmits. WebTransportServer and WebTransportTransport run over HTTP/3 and QUIC:
ChannelUnreliablepackets travel as independent QUIC datagrams (RFC 9221 / RFC 9297), so a lost packet never blocks newer unreliable updates and stale datagrams are not retransmitted. Each unreliable payload must fit inside the QUIC datagram MTU (typically ~1100–1200 bytes;SendreturnsErrPayloadTooLargepast it). When replicating over WebTransport, keepServerWorld.SetMaxSnapshotSizearound 1000 bytes.ChannelReliableOrderedpackets and the connection handshake travel on one client-opened bidirectional QUIC stream using 4-byte big-endian length-prefixed frames (up to 4 MiB each). Both channels work side by side without a relay, andWebTransportServerimplementsRelayServerso aRelaycan serve browser players over QUIC datagrams too.- On
js/wasm,WebTransportTransportcalls the browser'sWebTransportAPI directly throughsyscall/jsand does not linkquic-goorwebtransport-gointo the WebAssembly binary.
Browser support
WebTransport reached Baseline Newly Available across all major engines in March 2026:
| Browser | Minimum version |
|---|---|
| Chrome / Chromium | 97+ |
| Edge | 98+ |
| Firefox | 114+ |
| Safari / iOS Safari | 26.4+ |
Older Safari releases, embedded WebViews, and corporate networks that block UDP port 443 still need WebSocket as a fallback.
Certificates and local development
WebTransport always encrypts with TLS 1.3 (https://), so the handshake token and resume secret are protected in transit.
- Production: pass a CA-signed certificate (for example from Let's Encrypt) to
ListenWebTransportorListenWebTransportFiles, and leaveWebTransportOptions.ServerCertificateHashesempty. - Local development: browsers accept self-signed certificates when
WebTransportOptions.ServerCertificateHashescarries the 32-byte SHA-256 digest (sha256.Sum256(leafCertDER)) of the server's leaf X.509 certificate. Per the W3C WebTransport specification, self-signed pinned certificates must use an ECDSA (such as P-256) or Ed25519 key (or RSA) and have a validity period of at most 14 days.
srv, err := netplay.ListenWebTransportFiles(":4433", 1, auth, "cert.pem", "key.pem",
[]string{"https://play.example.com"})
// Local dev with a pinned self-signed cert hash (leave ServerCertificateHashes empty in production):
cli, err := netplay.DialWebTransport("https://localhost:4433/game", token, netplay.WebTransportOptions{
ServerCertificateHashes: [][]byte{certSHA256[:]},
})
Fallback to WebSocket
Use WebTransportSupported or DialWebTransportWithFallback to feature-detect WebTransport and fall back to WebSocket automatically:
tx, err := netplay.DialWebTransportWithFallback(
"https://play.example.com:4433/game",
"wss://play.example.com/game",
token,
netplay.WebTransportOptions{},
)
- When
WebTransportSupported()is false (such as an older browser),DialWebTransportWithFallbackdialswsURLwithDialWebSocketimmediately. - On native targets, a connection failure (other than
ErrRejectedorErrTokenTooLarge) falls back toDialWebSocket. - In browsers,
DialWebTransportreturns immediately withState() == ConnConnecting. If UDP/443 is blocked by a firewall, the session moves toConnDisconnectedwithLocalID() == 0and an error that does not matchErrRejected; close it and callDialWebSocket(wsURL, token)as a runtime fallback.
Inbox limits
A transport keeps each packet that arrives until Recv takes it, and bounds what it keeps by channel and by peer. Each peer's ChannelUnreliable packets are capped at DefaultUnreliableInbox (4096): when one arrives from a peer that already has that many queued, that peer's oldest queued unreliable packet is dropped, so a game that falls behind keeps the newest state, a peer that floods a server evicts only its own packets, and InboxStats().UnreliableDropped counts the loss.
ChannelReliableOrdered packets are never dropped, since that would break the channel's guarantee. A transport whose peers cannot be trusted caps each peer's reliable packets at DefaultReliableInbox (16384) instead and refuses the one that goes past it, counting it in InboxStats().ReliableRefused: a WebSocketServer or WebTransportServer ends that client's session, as if it had left, so the packets it did deliver stay in order, and a RelayTransport stops acknowledging that peer, which resends its messages once Recv has made room. The other transports receive reliable packets only from their server or from the same process and keep them all. Every transport also counts InboxStats().ReliableWarnings once each time its whole reliable backlog passes DefaultReliableInboxWarning (4096), and ReliableBacklog stays true until the backlog falls to half of it, so a queue hovering at the threshold does not warn on every packet. Recv still returns packets in arrival order across both channels and every peer. UDPTransport never sends reliable packets, so it drops a datagram that claims to be one.
Every transport implements InboxReporter: SetInboxLimits changes the limits at any time (lowering the unreliable cap drops each peer's oldest unreliable packets at once), and InboxStats reads the counters, which only grow, so a game or a debug overlay compares them with the previous frame's. LoopbackConfig.Inbox sets the limits of the peers a hub adds, and RelayTransportConfig.Inbox those of a RelayTransport. In InboxLimits, zero uses the default and a negative value removes the cap or turns the warning off. A RelayTransport's inner transport has its own limits, and every relayed peer arrives on it from the relay, so they share its per-peer cap; a reliable message it drops there is sent again.
tx.SetInboxLimits(netplay.InboxLimits{Unreliable: 512, ReliableWarning: 1024})
var lastWarnings, lastDropped uint64
// Each frame, after draining Recv:
st := tx.InboxStats()
if st.ReliableWarnings != lastWarnings {
log.Printf("reliable backlog of %d packets", st.Reliable)
}
if st.UnreliableDropped != lastDropped {
log.Printf("dropped %d stale unreliable packets", st.UnreliableDropped-lastDropped)
}
lastWarnings, lastDropped = st.ReliableWarnings, st.UnreliableDropped
Outbox limits
WebSocketServer.Send and WebTransportServer.Send never write to a client's connection on the caller's goroutine. Each client has an outbox: Send encodes the frame, queues it and returns, and a goroutine of that client's own writes the queue out in order, so a player on a slow link, or a client that stops reading, delays only its own frames and never the match's tick or the other players. WebTransportServer gives each client two outboxes with the same limits: one for the ChannelReliableOrdered frames, which share one QUIC stream, and one for the ChannelUnreliable datagrams, so a client that stops acknowledging QUIC packets cannot hold Send either. Send still checks a datagram's size when it queues it and returns ErrPayloadTooLarge at once.
Each outbox holds at most DefaultOutboxFrames (256) frames and DefaultOutboxBytes (4 MiB) of them. A frame that does not fit first makes room by dropping that client's oldest queued ChannelUnreliable frames, counted in OutboxStats().UnreliableDropped; an unreliable frame that still does not fit is dropped itself. A ChannelReliableOrdered frame that does not fit ends the client's session, because dropping it would break the channel's order: NextEvent reports PeerLeft with Reason "outbox full", Send returns ErrDisconnected for that client, and OutboxStats().Overflows counts it. A frame always fits an empty outbox, whatever its size. A write that fails, or takes longer than the 10-second write timeout, closes the client's connection instead of leaving part of a frame on the wire, and NextEvent reports PeerDropped as for any lost connection.
Because Send returns before anything is written, it reports only ErrDisconnected, ErrUnknownPeer, ErrPayloadTooLarge and ErrClosed. A write that fails later is reported by NextEvent as PeerDropped, never by Send, and the frames still queued for that client are discarded. WebSocketServer.Kick and Close let each client's writer flush the frames already queued for it, then the close frame, so a result or a goodbye sent just before them arrives; they return without waiting for the connection (Close waits at most a second for all clients together), and a client that has not taken its frames within a second is cut off.
Both servers implement OutboxReporter: SetOutboxLimits changes the limits at any time, for the frames queued afterwards, and OutboxStats reports the frames and bytes queued across the connected clients with the counters, which only grow. In OutboxLimits, zero uses the default and a negative value removes the cap. Raise the limits for a game that sends large reliable bursts, such as a level's initial state, rather than remove them: without a cap a client that stops reading holds every frame sent to it in the server's memory until the write timeout closes its connection.
srv.SetOutboxLimits(netplay.OutboxLimits{Frames: 512, Bytes: 8 << 20})
st := srv.OutboxStats()
log.Printf("%d frames (%d bytes) queued, %d sessions ended for overflow", st.Frames, st.Bytes, st.Overflows)
Connection handshake and reconnect
WebSocketServer, WebTransportServer, and UDPServer admit a client only after a handshake: the client presents an app-defined token, Auth.Verify checks it, and the server assigns the client's PeerID and sends it back, or refuses with a reason. A refused client never reaches Recv or Peers.
auth := netplay.Auth{
// Check a signed token locally; the error text is sent to the client.
Verify: func(peer netplay.PeerID, token []byte) error {
if !validToken(token) {
return errors.New("invalid token")
}
return nil
},
GracePeriod: 15 * time.Second, // a dropped client keeps its ID this long
}
srv, _ := netplay.ListenWebSocket(":8080", 1, auth) // or netplay.ListenUDPServer(":7777", 1, auth)
// Each server tick:
for ev, ok := srv.NextEvent(); ok; ev, ok = srv.NextEvent() {
switch ev.Kind {
case netplay.PeerJoined: // new ID
case netplay.PeerDropped: // connection lost; ID and state kept for GracePeriod
case netplay.PeerResumed: // same ID back within GracePeriod
case netplay.PeerLeft: // closed, GracePeriod ran out, Kick removed it, or its outbox overflowed (then ev.Reason says why)
rooms.Leave(roomOf(ev.Peer), ev.Peer)
guard.Reset(ev.Peer)
}
}
cli, err := netplay.DialWebSocket("wss://play.example.com/game", token) // or netplay.DialUDP(addr, serverPubKey, token)
if errors.Is(err, netplay.ErrRejected) {
// err is a *netplay.RejectError carrying the server's reason
}
me := cli.LocalID() // assigned by the server
// After a network drop (State() == netplay.ConnDisconnected):
_ = cli.Reconnect() // cli.Resumed() reports whether it kept its ID
NextEventchecks grace periods and idle timeouts when it is called, so a server polls it every tick alongsideRecv. Queued lifecycle events (PeerJoined,PeerDropped,PeerResumed,PeerLeft) drain in FIFO order in constant amortized time per event without silently dropping any. While a peer is inside its grace period,Sendto it returnsErrDisconnectedandPeersleaves it out.Closeon a client is a goodbye: the server reportsPeerLeftat once instead of holding the session.Reconnectis not, even while the connection is still open: the browser closes its old socket with a code the server counts as a drop.WebSocketServer.Kick(peer, reason)andUDPServer.Kick(peer, reason)end one peer's session now and reportPeerLeftwith the reason inPeerEvent.Reason: the session's resume secret is gone, so the client'sReconnectcannot resume it and joins as a new peer only ifVerifyaccepts its token again. A connected WebSocket client receives the frames already sent to it and then a close frame with the application close code 4001 and the reason, a connected UDP client an encrypted kick datagram sent three times (UDP may drop one); both then report a*KickError, matchingnetplay.ErrKicked, fromErr— in the browser too. A peer inside its grace period gets no message; its next handshake simply cannot resume.KickreturnsErrUnknownPeerfor an ID with no session.- A server holds at most
Auth.MaxPeerssessions (1024 by default), counting peers inside their grace period; past it, clients are rejected with the reasonserver full. WithoutVerifyanyone may join, so set it on any server reachable from the internet. - A resume proves itself with a secret from the previous welcome, which the server replaces on every handshake; an ID alone cannot take over a session. Past the grace period,
Reconnectjoins again as a new peer when the token is still valid. - In the browser,
DialWebSocketandDialWebTransportreturn at once:StateisConnConnectingandLocalIDis 0 until the welcome arrives, and packets sent before then are queued. A refusal moves them toConnDisconnectedwith the*RejectErrorinErr. - A
WebTransportServerderives its QUICMaxIdleTimeoutfromAuth.IdleTimeout(10 seconds by default, never less than 3) and sends QUIC keepalive pings every third of it; a nativeWebTransportTransportpings every 2 seconds with a 10-second idle timeout. Closing withClosesends a goodbye (PeerLeftat once);Reconnectcloses with code4000so the server keeps the session forAuth.GracePeriod. - A
WebSocketServerpings each client every third ofAuth.IdleTimeout(10 seconds by default, never less than 3) and drops a client from which no byte arrives, not even a pong, for that long, so a client whose network vanished without closing the TCP connection does not hold its session and ID forever. Browsers answer pings on their own. A nativeWebSocketTransportpings the server every 2 seconds and moves toConnDisconnectedwhen no byte arrives for 10 seconds. A large frame that arrives slowly is read whole as long as its bytes keep coming. These timeouts use the wall clock, notAuth.Now.WebSocketServer.Closesends every client its close frame at once and waits at most a second for clients that do not read. - A WebSocket frame's payload is read as it arrives rather than allocated from the length the frame announces, and a hello may be no longer than a valid one, so a client that announces a large frame and then trickles it cannot reserve memory it has not sent. Control frames (
Close,Ping,Pong) follow RFC 6455 §5.5: fragmented control frames, control frames over 125 payload bytes, and reserved control opcodes (0xB–0xF) are rejected. - UDP sessions run handshake version 3 using
Noise_NK_25519_ChaChaPoly_SHA256:DialUDP(addr, serverKey, token)authenticates the server against its static X25519 public key without certificates, encrypts the client's token and resume secret in message 1, and receives the assigned ID and fresh resume secret encrypted in message 2 (the welcome carries no packet key). Hellos of versions 1 and 2 receive the existing version rejection, so clients and servers must be upgraded together. - Before message 1 a UDP client asks for a cookie and the server answers with one: an HMAC of the client's address, its handshake nonce and a 5-second time window under a random key, for which the server keeps no state. Message 1 carries the cookie and binds it into the Noise prologue. Only a hello with a cookie issued to its source address in the current or previous window is decrypted, reserves a session slot and reaches
Auth.Verify; any other hello gets a fresh cookie, smaller than the hello. A sender with a spoofed source address never sees its cookie, so it cannot fillAuth.MaxPeersor make the server runVerify, and a captured hello replayed from another address does not decrypt. A hello replayed from the client's own address while its cookie is valid joins nobody: the server ignores a nonce it admitted in the last two windows, and a client that rebuilt its hello for a newer cookie is rebound to its existing session rather than admitted twice.DialUDPandReconnectrun this round trip themselves; it adds one round trip to each handshake. - Distribute the server's static X25519 public key out of band: either set a persistent
Auth.StaticKey(*ecdh.PrivateKeyfromecdh.X25519()) when clients load the key from configuration or reconnect across server restarts, or leaveAuth.StaticKeynil to generate a fresh key pair per server and shipsrv.PublicKey().Bytes()in matchmaking tickets (parsed on the client withecdh.X25519().NewPublicKey). Missing or non-X25519 keys fail withErrInvalidKey. - After the UDP handshake, every datagram is encrypted and authenticated with ChaCha20-Poly1305 using the two directional keys from the Noise split: a 10-byte cleartext header (
type,peer,seq,channel) is authenticated as associated data, followed by ciphertext and a 16-byte Poly1305 tag (26 bytes of overhead), with an implicitdirection || peer || seqnonce and a 64-entry replay window. Sending or receiving refuses to wrap the 32-bit sequence counter (ErrSequenceExhausted); callingReconnectruns a fresh Noise NK handshake to rekey the session and reset sequence numbers. AUDPClientpings every second while idle; a client silent forAuth.IdleTimeout(10 seconds by default, never less than 3) counts as dropped. The server answers only hellos of at least 128 bytes and never replies with more than it received. - On
UDPTransport,AddPeerKey(id, addr, key)protects direct peer-to-peer datagrams with a 32-byte pre-shared key using the same 26-byte ChaCha20-Poly1305 framing, replay window, reflection protection and sequence-wrap refusal (re-registering with a fresh key rekeys the peer); plaintext datagrams claiming a keyed peer's ID are dropped. PlainAddPeerremains unauthenticated and unencrypted for trusted LAN use and tests. - The WebSocket handshake (version 1) is not encrypted at the application layer, so use
wss://(ListenWebSocketTLS/ListenWebSocketTLSFiles) in production.
Server-authoritative delta replication
Use ServerWorld and ClientWorld for action, co-op, or arena games where the server owns authoritative entity state. ServerWorld.Step(tick) tracks per-peer acknowledged snapshots and sends compact field-level deltas (falling back to full snapshots when needed):
// Server:
server := netplay.NewServerWorld(serverTx)
heroID := server.Spawn(netplay.EntityState{
ComponentState: netplay.ComponentState{Kind: 1, X: 100, Y: 50},
})
server.Update(heroID, updatedState)
_ = server.Step(tick)
// Client:
client := netplay.NewClientWorld(clientTx, serverID)
client.OnSpawn = func(e netplay.EntityState) { /* attach visuals */ }
client.OnDespawn = func(id netplay.EntityID) { /* remove visuals */ }
client.Poll()
client.Predict(heroID, localCmd, applyInput)
renderEntities := client.Interpolate(renderTick) // smooth remote entity motion
_ = renderEntities
Inspect bandwidth and reconciliation behavior with server.Stats(), client.PredictedState(id), client.Reconciliations(), and client.LastReconcileError().
Stepnever passesTransport.Senda payload larger thanDefaultMaxSnapshotSize(60000 bytes, which fits UDP, relay and WebSocket packets), or the size set withserver.SetMaxSnapshotSize(n). A larger full snapshot is split into parts thatClientWorldreassembles before applying, and a delta larger than the limit is replaced by a split full snapshot. A lost part costs that snapshot only: the client does not acknowledge it, so the nextStepsends a whole one again.Stats().PacketsSentcounts parts,SnapshotsSentwhole snapshots. An entity that does not fit in a part on its own makesStepreturnErrPayloadTooLargenaming it.- Ticks are
uint32and compared in serial-number order, so replication keeps working when the counter wraps after 2^32 ticks (about 2.27 years at 60 Hz).InterpolatereadsrenderTickrelative to the latest snapshot's tick, modulo 2^32, sofloat64(latest) - delayworks on either side of the wrap. A server that starts its ticks over in the same epoch sends ticks the client takes as stale: callclient.Reset(), which forgets every snapshot, prediction and the epoch and reports each known entity throughOnDespawn, before the new match's first snapshot, or start the match withserver.SetEpochso clients follow on their own.
Replication epoch
Every snapshot carries the server's replication epoch, a number NewServerWorld picks at random, so a restarted server's snapshots are told apart from the previous run's even when their ticks overlap. A ClientWorld that receives a complete full snapshot of a new epoch forgets every snapshot, partial snapshot and prediction of the old one, reports each entity it knew through OnDespawn, and continues from the new epoch, without the game calling Reset. The snapshot is parsed whole first, so a corrupt packet cannot wipe the world. A delta of a new epoch cannot be applied to anything the client holds, so it only makes the client ask the server for a full snapshot of that epoch. The client remembers the last 16 epochs it left and ignores their snapshots, so a snapshot of the old run that arrives late cannot pull the client back. client.Epoch() returns the epoch the client follows.
- A server that keeps running but starts a new match with its ticks back at zero calls
server.SetEpoch(server.Epoch() + 1): the nextStepsends every peer a full snapshot, and clients drop the old match as they would after a restart. A server that numbers its runs itself, or a test that needs fixed epochs, sets one the same way. An epoch should not repeat, because clients ignore the epochs they have left until 32 of their snapshots arrive in a row. - Random epochs cannot be ordered, so a late snapshot of an epoch the client never saw, arriving after the live epoch's first one, moves the client to that dead epoch. A left epoch that keeps sending, 32 snapshots in a row with none of the current epoch between them, is therefore taken as the live one and followed again; a late burst of a finished run is far shorter. The same recovers a restart that draws one of the 16 left epochs.
- Two runs pick the same random epoch with a chance of one in 2^32; such a restart looks like a server that went back in ticks, which
ResetorSetEpochhandles. - Acknowledgments carry the epoch too, and the server ignores those of another epoch, so a client's ack of the old run never becomes a delta baseline in the new one.
Wire format
Replication speaks version 2 of its wire protocol (ReplicationVersion); version 2 added the epoch. Every message starts with one byte holding the protocol version in its high four bits and the message type in its low four, then the epoch as 4 little-endian bytes. uv is an unsigned varint as encoding/binary writes it.
| Message | Type | Direction | Layout after the 5-byte header |
|---|---|---|---|
| Full snapshot | 1 | server to client | tick(uv) count(uv) entity... |
| Delta snapshot | 2 | server to client | tick(uv) baselineTick(uv) despawnCount(uv) id(uv)... updateCount(uv) entity... |
| Acknowledgment | 3 | client to server | tick(uv) |
| Resync request | 4 | client to server | nothing |
| Full snapshot part | 5 | server to client | tick(uv) index(uv) total(uv) count(uv) entity... |
An entity is id(uv) mask(1) followed by the fields whose mask bit is set, in bit order: kind (uv), x, y, vx, vy and rotation (4-byte little-endian floats), flags (uv), and props (a uv length, then the bytes). A full snapshot sets the bit of every field that is not zero; a delta sets the bits of the fields that changed since the baseline, and of every non-zero field of an entity the baseline did not have.
Version 1, from releases before the epoch, had no header: its messages start with the bare type 1 to 5, which reads as version 1. Peers of different versions cannot replicate to each other, and neither misreads the other's messages. A client refuses snapshots of another version and reports it through client.Err(), a server refuses acknowledgments and resync requests of another version and reports it through server.PeerErr(peer); both errors wrap ErrReplicationVersion and name the two versions, so a game can tell the player to update instead of waiting for a world that never arrives.
Every pkg/online/netplay wire format—transport frames, UDP sealed datagrams, connection handshake, relay frames and control bodies, replication v2, lockstep/rollback inputs, and mid-match membership—is also specified as a versioned YAML schema under pkg/online/netplay/wire/ (*.wire.yaml), with zero-allocation Go codecs (AppendX / DecodeX, plus DiffX / ApplyX for delta masks) and dependency-free TypeScript codecs (encodeX / decodeX, plus diffX / applyX) generated by game wire. Games can define their own schemas for typed EntityState.Props or custom messages with bit packing and integer-only qfloat/fixed quantization; see netplay/wire.
Cross-language clients
Web dashboards, spectator views, bots, and ports rendered in another engine can speak every pkg/online/netplay protocol directly in TypeScript without hand-writing binary parsers. game wire -lang ts generates deterministic, dependency-free TypeScript modules backed by a DataView over a caller-owned Uint8Array that run identically in browsers, Node, and Bun:
go run ./cmd/game wire -lang ts -in pkg/online/netplay/wire -out pkg/online/netplay/wire/ts
The committed TypeScript codecs for all seven built-in protocols live under pkg/online/netplay/wire/ts/ and are checked in CI with -check. Every message X generates createX(), encodeX(m: X, w: Writer): void, and decodeX(r: Reader, m: X): void (filling a reusable object so a 60 Hz client avoids per-packet allocations), and delta mask groups generate diffX(base, cur) and applyX(base, delta):
import {
Reader,
ReplicationMessageTagFull,
Writer,
createReplicationMessage,
decodeReplicationMessage,
encodeReplicationMessage,
} from "./pkg/online/netplay/wire/ts/index.ts";
const msg = createReplicationMessage();
const reader = new Reader(packetBytes);
decodeReplicationMessage(reader, msg);
if (msg.tag === ReplicationMessageTagFull) {
console.log(msg.full.epoch, msg.full.tick, msg.full.entities.length);
}
- 64-bit unsigned and signed fields (
u64,i64, 64-bituvarint/svarint, andbitswider than 32) map tobigint, so values aboveNumber.MAX_SAFE_INTEGERsuch asInputFrame.bits,ChecksumMsg.checksum, andRelayAck.maskremain exact. qfloatandfixedquantize and dequantize with pureBigIntinteger arithmetic matching Go'swire.QuantizeFloat/wire.DequantizeFloat/wire.QuantizeFixedbit for bit, anddiffXcompares IEEE-754 float bit patterns (-0.0differs from+0.0,NaNequalsNaN).- Shared YAML golden vectors under
pkg/online/netplay/wire/testdata/vectors/are verified in both Go and TypeScript (bun test pkg/online/netplay/wire/testdata/ts) on every PR, alongside a live end-to-end test where a GoServerWorldsends full, delta, and split snapshots to the TypeScript decoder. - See
examples/ts-replication-client/for a complete TypeScript client that decodes a replication v2 stream (SnapshotFull,SnapshotDelta, andSnapshotFullPart) and encodesSnapshotAck/ResyncRequestreplies.
Deterministic lockstep and rollback netcode
For fighting games, RTS titles, or tight arcade co-op, implement SimState (Step, Save, Load, Checksum) and drive it with LockstepSession or RollbackSession:
session := netplay.NewRollbackSession(netplay.RollbackConfig{
Transport: peerTx,
Sim: sim,
Peers: []netplay.PeerID{1, 2},
MaxRollback: 8,
InputDelay: 1,
ChecksumInterval: 30,
OnDesync: func(ev netplay.DesyncEvent) {
log.Printf("desync at tick %d: local=%x remote=%x", ev.Tick, ev.LocalChecksum, ev.RemoteChecksum)
},
})
// Each fixed step in game.Loop.Update:
if err := session.Poll(); err != nil {
return err
}
err := session.Step(netplay.InputFrame{Tick: session.Tick(), Peer: peerTx.LocalID(), Bits: buttons})
if err != nil && !errors.Is(err, netplay.ErrRollbackStalled) {
return err
}
RollbackSessionpredicts missing remote inputs, saves snapshots per tick, and automatically rewinds and resimulates when a late remote input disagrees with prediction. It never runs more thanMaxRollbackticks past the oldest tick still waiting for a remote peer's input: thereStepsimulates nothing and returnsErrRollbackStalled, which matchesErrRollbackWindowExceededwitherrors.Is, until the input arrives or the host removes the peer, so a peer that stops sending stalls the match instead of growing its history every tick. The error names each peer the session waits for, andWaiting()returns their IDs. A stalled session sends a peer nothing more, so an unreachable peer it waits for is never reported withErrSessionBacklog: the host removes a peer that keeps the match stalled for longer than the game tolerates. TreatErrRollbackStalledas a frame to wait, as above; any otherErrRollbackWindowExceeded, a late input that disagrees with a tick beyond the window, is fatal to the match.LockstepSession(SubmitInput,Poll,Step) advances only when every peer's input for the current tick has arrived, ideal for RTS or turn-synchronized play.SubmitInputignores an input for a tick already run or more than 256 ticks ahead.- Both sessions keep checksums for a bounded window only: a lockstep session ignores a peer's checksum for a tick more than 256 ticks behind or ahead of its own, and a rollback session one for a tick older than its checksum history (256 ticks, or further back while ticks there wait for confirmation) or more than 512 ticks ahead.
- Both sessions exchange periodic checksums (use
ChecksumByteshelper) to surface determinism bugs immediately viaOnDesyncandDesyncs(). A session compares only the checksum ticks, those itsChecksumIntervalsends for, and ignores a peer's checksum for any other tick.OnDesyncandRollbackSession'sStats().Desyncssee every desync, whileDesyncs()returns only the latest 256. - Both sessions compare
uint32ticks in serial-number order (RFC 1982), including membership windows and pending joins/leaves, so long matches continue seamlessly across the 2^32 tick wraparound.RollbackSessionkeeps unconfirmed history until the first tick confirms, so a peer whose first input arrives after the session has stalled can still confirm and advance.
Players joining and leaving mid-match
A match's peers are not fixed when the session is created. One peer, the host (Host in the config, the lowest peer ID by default), admits and removes peers while the match runs, and every peer applies each change at the same tick:
// On the host, when its transport reports a new player (PeerJoined, or a new ID in RelayTransport.Peers):
joinTick, err := session.AddPeer(newPeer)
// On the new player's machine; Step it every frame as usual, it only polls until Active reports true:
session := netplay.NewRollbackSession(netplay.RollbackConfig{Transport: peerTx, Sim: sim, Host: hostID, Join: true})
// On the host, once its transport reports the player gone (PeerLeft, or the ID missing from Peers):
leaveTick, err := session.RemovePeer(oldPeer)
- The join tick is the one the host's next own input is for: the later of
Tickand the tick after its latest submitted input for lockstep,Tick + InputDelayfor rollback. No peer can have run (lockstep) or confirmed (rollback) that tick yet, since that takes the host's input for it, and the change travels on the reliable channel ahead of that input. A rollback peer that has already predicted past the tick rolls back to it. - Once the host's simulation stands at the start of the join tick (lockstep), or every tick before it is confirmed (rollback), the host sends the newcomer
Sim.Save()at that tick together with the tick, the peers and, for each peer already leaving, the inputs the host declared for it from the join tick to its leave, in parts of 48000 bytes. The newcomer loads the state into its ownSim, itsTickstarts at the join tick, and from then on it plays like the others, including the ticks of a peer the host removed before the newcomer had its state. A state larger thanMaxJoinStateSize(16 MiB) makesAddPeerreturnErrPayloadTooLarge, and one that has grown past it by the join tick turns the join into a leave, so nobody waits for the newcomer. - Inputs before the join tick are not expected from the newcomer. In rollback its first
InputDelayticks are zero inputs on every peer, as every peer's are at the start of a match. Each peer sends the newcomer the inputs it had already sent for the join tick and later, and an input from the newcomer that reaches a peer before the host's announcement is kept until the announcement arrives. - The leave tick is the host's next own input tick, or the peer's join tick if that is later. The change carries the inputs the host holds from the leaving peer for the ticks before it, and a zero input for each tick the host never received from it, so every peer runs those ticks alike; inputs arriving straight from the leaving peer are ignored from then on. Remove a peer after it has stopped sending: one removed while still sending may have reached another peer with an input the host never saw, which shows up as a desync. The removed peer's own session stops at the leave tick, and its
Activereports false. Memberslists the peers taking part in the current tick. Only the host may callAddPeerandRemovePeer(ErrNotHostelsewhere), the host cannot leave (ErrHostLeave), and a peer that left comes back under a new ID (ErrPeerRejoin, however long ago it left), as relays and servers assign one.RemovePeerof a peer that left returns its leave tick.- A message the transport cannot deliver yet, because a relay has not told the sender of the newcomer or a peer is reconnecting, waits in the session and is sent again by the next
Poll, in order with the messages that follow it. A session keeps up to 4096 such messages per peer; past that the peer has missed part of the match, every later message to it is dropped, and the nextSubmitInput(lockstep) orSteporPoll(rollback) returnsErrSessionBacklogonce for it, so the host removes it or the match ends. - A joining session finds its host in
Host, or else as the lowest other ID inPeers; when neither names another peer it takes the lowest other ID its transport reports, on eachPoll, until its join state arrives. Over a relay, whose peer list may still be empty or include other newcomers, setHostexplicitly.
Packet capture and inspection
netplay.NewCaptureTransport(inner, w, opts) wraps any Transport and records each successful Send and each packet returned by Recv as one versioned JSON line (version: 1):
{"version":1,"t":16000000,"dir":"send","peer":2,"ch":1,"seq":1,"payload":"IwEAAAAB"}
f, _ := os.Create("session.capture.jsonl")
defer f.Close()
tr := netplay.NewCaptureTransport(inner, f, netplay.CaptureOptions{
Channels: []netplay.Channel{netplay.ChannelUnreliable},
Peers: []netplay.PeerID{2},
SampleEvery: 1,
})
- Plaintext above encryption. Because
NewCaptureTransportwraps theTransportinterface, wrapping aUDPClientorUDPServercaptures plaintext payloads after authentication and decryption, so offline inspection never needs session keys. - Filtering and sampling.
CaptureOptionscan restrict capture to specificChannelsorPeers, stride-sample one in everySampleEverymatching packets, or deterministically sample withSampleRateandSampleSeed. Capture write errors are reported viaOnErrorand never failSendorRecv. - InboxReporter passthrough. The wrapper forwards
SetInboxLimitsandInboxStatsto the underlying transport. - Privacy warning. A packet capture contains raw player inputs, entity positions, and game state. Never ship capture files, commit player sessions, or collect captures from players without explicit consent.
Decoding captures with game net
The game net CLI subcommand uses the wire schemas to decode capture logs layer by layer (transport header, relay frame and control body, then replication, lockstep/rollback, membership, handshake, or custom game messages) and to compute traffic summaries:
# Decode a capture log layer by layer as YAML or JSON
go tool game net decode session.capture.jsonl
go tool game net decode --format json session.capture.jsonl
# Decode custom EntityState.Props using your game's *.wire.yaml schemas
go tool game net decode --schema ./proto session.capture.jsonl
# Track per-peer replication baselines so SnapshotDelta frames also print full resolved entity state
go tool game net decode --resolve-deltas session.capture.jsonl
# Decode a single hex or base64 frame from standard input
echo "230100000001" | go tool game net decode -
# Print packet and byte totals per message type, per peer, and mean delta size
go tool game net stats session.capture.jsonl
Live network debug overlay
For live in-game inspection without per-frame allocations, wrap your transport with gamedebug.NewNetMonitor and pass mon.Snapshot to gamedebug.Options.Net. The overlay panel displays bytes/s and packets/s per channel and per peer, InboxStats counters (queued, dropped, refused, warnings), and the most recent decoded message types using reused wire codec buffers. See gamedebug.