← Back to Docs
  • index.html
  • Clayground
  • Clayground Network Module
  • Clayground 2026.14
  • Clayground Network Module

    The Network module provides peer-to-peer connectivity using WebRTC, enabling multiplayer games and distributed applications without dedicated server infrastructure.

    WebRTC Technology

    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.

    P2P Architecture

    All communication flows directly between peers after initial connection setup. This approach is ideal for:

    Connection Establishment

    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.

    ICE Server Configuration

    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" }
        ]
    }

    Scalability Considerations

    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 Modes

    Signaling is only needed for initial peer discovery - once connected, all data flows directly between peers.

    Joining

    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.

    Replicated Objects

    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.

    Troubleshooting

    ProblemLikely CauseSolution
    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 otherAsymmetric NAT types between peersAdd a TURN server; check diagnosticMessage for candidate types
    Only "host" candidates, no "srflx"STUN server unreachable or blockedCheck firewall, try different STUN servers
    "Network full" errormaxNodes limit reachedIncrease maxNodes or wait for a slot
    Connection times outSignaling server unreachable or NAT blocks all pathsEnable verbose mode, check diagnosticMessage for which phase fails

    External Resources

    Components

    ClayHttpClient

    Configurable HTTP client with automatic API method generation

    Network

    Unified P2P networking for games, apps, and distributed systems