The Network module provides peer-to-peer connectivity using WebRTC, enabling multiplayer games and distributed applications without dedicated server infrastructure.
WebRTC (Web Real-Time Communication) is a browser-native technology that enables direct peer-to-peer connections. Clayground uses WebRTC Data Channels for reliable and ordered message delivery between nodes.
On Desktop and Mobile, WebRTC is implemented via libdatachannel. In browsers (WASM), the native WebRTC APIs are used directly.
All communication flows directly between peers after initial connection setup. This approach is ideal for:
Connecting two peers involves three phases:
Enable verbose mode and watch the diagnosticMessage signal to see which candidate types are discovered and how long each phase takes.
By default, two Google STUN servers are used. For connections across restrictive NATs (symmetric NAT, carrier-grade NAT), configure TURN servers via the iceServers property:
Network { iceServers: [ "stun:stun.l.google.com:19302", { urls: "turn:relay.example.com:3478", username: "user", credential: "pass" } ] }
Warning: P2P networking is not suitable for large-scale applications. Each node must maintain connections to other nodes, and message relay overhead grows with the number of participants.
For games requiring more than 8 players or MMO-style architectures, consider dedicated server solutions instead.
Signaling is only needed for initial peer discovery - once connected, all data flows directly between peers.
Network.SignalingMode.Cloud) - Uses a PeerJS server for peer discovery: the public cloud server by default, or a self-hosted relay via signalingUrl (clay-dev-server ships one, so this mode also works offline on a LAN). Works across platforms (Browser, Desktop, Mobile). A native node checks a wss server's certificate unless verifySignalingCertificate is false. A dropped signaling connection is reported with signalingLost() and reconnected under the same ID; the network goes on meanwhile.Network.SignalingMode.Local) - Host runs an embedded signaling server; the network code carries its IP, port and a join secret. Works on a local network without internet. Desktop and Mobile only (not available in browser).Joining starts with a handshake over the encrypted data channel, in both signaling modes and on every platform: the joiner sends its wire version, appId, room password and clientToken, and the host takes it or refuses it. A refused joiner gets joinRefused() with "incompatible-version", "incompatible-app" or "wrong-password" and never becomes a node. The host keeps each joiner's token in clientTokens.
Objects that come and go - enemies, items, avatars - live in one table on every node: spawn() creates one with a type, props and an owner, only the owner's state for it counts, the host orders spawns, despawns and owner changes, and a node that joins late gets every live object with its last state and the session properties. An object whose owner leaves despawns or passes to the host, as it was spawned. ReplicatedObject binds an item's properties to an object, and Replicas makes an item per object of a type.
| Problem | Likely Cause | Solution |
|---|---|---|
| Connection hangs in "Connecting" (ice phase) | Both peers behind restrictive NATs (symmetric/carrier-grade) | Add a TURN server to iceServers |
| Works one direction but not the other | Asymmetric NAT types between peers | Add a TURN server; check diagnosticMessage for candidate types |
| Only "host" candidates, no "srflx" | STUN server unreachable or blocked | Check firewall, try different STUN servers |
| "Network full" error | maxNodes limit reached | Increase maxNodes or wait for a slot |
| Connection times out | Signaling server unreachable or NAT blocks all paths | Enable verbose mode, check diagnosticMessage for which phase fails |
Configurable HTTP client with automatic API method generation | |
Unified P2P networking for games, apps, and distributed systems |