Skip to content

Network API

This page documents network classes and Engine connection operations. Network transport is not an ActionRegistry category and does not define invented network:* actions.

Engine integration

engine.connect(url) wires a PeerConnection to the game document through the sync manager (ConsensusCore); with useWebRTC: true, it uses HybridPeerManager. The transport exchanges Automerge sync messages, not game command histories. Use engine.disconnect() before compact(), resume(), or other operations that require a detached document.

Classes

  • PeerConnection(url, engine?, options?): WebSocket client with connect(), disconnect(), sendToPeer(), broadcast(), peer IDs, and reconnect buffering.
  • HybridPeerManager(options): WebSocket/WebRTC upgrade manager with connect(), disconnect(), sendToPeer(), broadcast(), upgradeToWebRTC(peerId), and peer inspection methods.
  • WebRTCConnection(remotePeerId, config?): one encoded DataChannel with offer/answer/ICE methods, send, close, and connection state.
  • MessageCodec(config?): JSON or MessagePack encode/decode with optional compression. Encryption framing is reserved and currently unimplemented.
  • SyncTransport: core-owned typed CRDT port. PeerConnection and HybridPeerManager implement its peer-open, peer-close, frame, and accepted send operations; sync wire envelopes are V1 and are not net:message.
  • UniversalRelayServer(options?): stateless relay with start(), stop(), room routing, health endpoints, and rate limiting.
  • AuthoritativeServer(engine, options?): server-owned Engine and JSON describe/dispatch/history protocol. RoomAuthoritativeServer extends this model with room Engines.

All listed network classes are deep imports under network/; they are not re-exported by core/index.ts.

Experimental relay admission

UniversalRelayServerOptions.admissionProvider?: RelayAdmissionProvider is an experimental, generic extension seam. The provider signature is:

interface RelayAdmissionContext {
  operation: "connect";
  connectionId: string;
  remoteAddress?: string;
}

interface RelayAdmissionDecision {
  allowed: boolean;
  reason?: string;
}

interface RelayAdmissionProvider {
  authorize(
    credential: unknown,
    context: RelayAdmissionContext,
  ): Promise<RelayAdmissionDecision>;
}

PeerConnectionOptions.getAdmissionCredential?: () => Promise<unknown> and EngineNetworkOptions.getAdmissionCredential obtain an opaque, fresh credential for each socket open/reconnect. The client sends one encoded { type: "admission", credential } frame while admission is pending. A configured provider must allow it before welcome, peer rosters, room operations, routing, or WebRTC signaling; provider failures and timeouts fail closed. The reserved frame is never relayed, even without a provider. Without an admission provider, the relay remains exactly open legacy behavior; without a client callback, no admission frame is sent.

This seam does not provide game-rule validation, player/human identity, peer-key or ECDH binding, secure transport or encrypted sync, room authorization, replay protection or key rotation, or fair play. TLS/WSS is a separate deployment concern. No external credential service or SDK integration is implied.