← Back to Docs
  • index.html
  • Clayground
  • Clayground Network Module
  • Clayground 2025.2
  • 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.

    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