Godot
Atyha in Godot: join a host by a short room code (HK4M-7TRW) over WebRTC, signalled through public Nostr relays, with an optional password (CPace).
Two layers:
| layer | what | where |
|---|---|---|
| signalling | a GDExtension wrapping the C library (../c): AtyhaHost, AtyhaJoiner, Atyha | src/, built into example/addons/atyha/bin/ |
| peers | GDScript on Godot's WebRTCPeerConnection implementing the DataChannel profile of spec section 9 | example/addons/atyha/atyha_peers.gd |
WebRTC itself comes from godotengine/webrtc-native (libdatachannel inside), pinned at 1.2.2-stable and fetched into example/addons/webrtc_native by tools/fetch_webrtc.sh (SHA-256 checked).
Tested on macOS (arm64) with Godot 4.7.2; built against godot-cpp 10.0.0-stable (commit 507ed9d8) with the Godot 4.3 API, so the same binary loads in 4.3 and later. MIT.
Build (macOS, Homebrew)
brew install cmake libsodium curl # curl: 8.11+ with WebSockets (macOS's own 8.7 has none)
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH="$(brew --prefix curl);$(brew --prefix)"
cmake --build build # -> example/addons/atyha/bin/libatyha.macos.template_debug.arm64.dylib
tools/fetch_webrtc.sh # -> example/addons/webrtc_native/
godot --headless --path example --import # once, registers the extensions-DGODOTCPP_TARGET=template_release builds the release library (exports use it); -DGODOTCPP_API_VERSION=4.7 builds against a newer API (the extension then needs that Godot). godot-cpp and libsecp256k1 are fetched by CMake at pinned commits/tags; pass -DFETCHCONTENT_SOURCE_DIR_GODOT-CPP=/path/to/godot-cpp to use a local checkout.
What gets linked: the C library (add_subdirectory(../c) with tests, examples and the libdatachannel backend off), its libcurl WebSocket backend, libsecp256k1 (static) and libsodium (static: libsodium.a, -DATYHA_GODOT_STATIC_SODIUM=OFF for the shared one). libcurl stays a shared library: on this Mac the dylib references /opt/homebrew/opt/curl/lib/libcurl.4.dylib, so as built it runs on machines with Homebrew's curl only. To ship it, copy libcurl.4.dylib (and its own dependencies: OpenSSL, nghttp2, …) next to it and rewrite the paths with install_name_tool, or link a static libcurl built with WebSockets, then sign with your Developer ID.
Other platforms (not built or tested here)
The extension needs libcurl 8.11+ with WebSockets (ws/wss in curl -V) and libsodium.
- Linux:
apt install cmake libsodium-devand a libcurl 8.11+ (Debian 13 ships 8.14, checkcurl -Vlistswss; Ubuntu 24.04's 8.5 is too old: build curl,-DCURL_DISABLE_WEBSOCKETS=OFFis the default since 8.11, and pointCMAKE_PREFIX_PATHat it). Then the same twocmakelines; the output islibatyha.linux.template_debug.x86_64.so. - Windows (MSVC):
vcpkg install curl[websockets,ssl] libsodiumand configure with-DCMAKE_TOOLCHAIN_FILE=<vcpkg>/scripts/buildsystems/vcpkg.cmake. The C library addsSODIUM_STATICfor MSVC. Shiplibcurl.dllbeside the extension. - Android / iOS: cross-compile libcurl (WebSockets on, a TLS backend, CA certificates:
atyha_curl_options.ca_filewhere the platform store is not used) and libsodium, then use godot-cpp's toolchains (-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake, or godot-cpp'scmake/ios.toolchain.cmake). iOS wants an.xcframework. An alternative there is the C library's libdatachannel WebSocket backend (atyha_libdatachannel.h); it needs your own libdatachannel build, since webrtc-native does not export the one inside it. - Web export: not provided. The C library has an Emscripten WebSocket backend, but a GDExtension on the web needs a
dlink-enabled export template; a GDScript port of the signalling over Godot'sWebSocketPeerwould be the simpler route there.
atyha.gdextension already lists the macOS, Linux, Windows and Android library names.
API (signalling)
All three classes come from the extension. Nothing runs in the background: call poll() every frame (the signals fire from inside it).
# helpers
Atyha.generate_code() # "HK4M7TRW"
Atyha.parse_code("hk4m 7trw") # "HK4M7TRW" ("" when it is not a code)
Atyha.display_code("HK4M7TRW") # "HK4M-7TRW"
Atyha.default_relays() # the relays of spec section 8
Atyha.version() # "1.0.0"
# host
var host := AtyhaHost.new()
# host.relays = PackedStringArray([...]) # optional, before open()
host.opened.connect(func(): print(host.get_display_code()))
host.offer_received.connect(func(attempt: String, sdp: String, meta: Dictionary):
# make the peer connection, gather, then:
host.answer(attempt, answer_sdp) # or host.reject(attempt, "room full")
)
host.open("my-game", "", "password or empty", ice_servers, {"name": "my server"}) # -> Error
# every frame: host.poll(); host.close() closes the room
# joiner
var j := AtyhaJoiner.new()
j.need_offer.connect(func(ice_servers: Array, host_meta: Dictionary):
# make the peer connection + both channels, gather, then:
j.set_offer(offer_sdp))
j.answered.connect(func(sdp: String): pc.set_remote_description("answer", sdp))
j.failed.connect(func(error_name: String, reason: String): ...)
j.join("my-game", "HK4M-7TRW", "password or empty", {"player": "p1"}) # timeout_ms: optional 5th
# every frame: j.poll(); j.cancel() -> failed("cancelled", ...)ice_serversis an Array of{"urls": [..] or "..", "username": .., "credential": ..}, the same formWebRTCPeerConnection.initialize({"iceServers": ...})takes. The host's list reaches joiners inneed_offer; an empty Array means "use your own".metais a Dictionary sent as a JSON object (at most 2048 bytes); an empty one sends nothing and an absent one arrives as{}.failederror names:bad_code,no_relay,no_room,wrong_password,rejected(reason = the host's text),no_answer,cancelled.- Both classes also emit
log_line(text)(relay connections, attempts). AtyhaHost:open(...),poll(),answer(attempt, sdp) -> bool,reject(attempt, reason) -> bool,close(),is_open(),get_code()(canonical),get_display_code().AtyhaJoiner:join(...),poll(),set_offer(sdp) -> bool,cancel(),is_done(),close().
One libcurl WebSocket backend is shared by every live host and joiner of the process; it is created by the first and freed with the last.
Peers: addons/atyha/atyha_peers.gd
A Node that runs both layers and hands over open channels:
const AtyhaPeers := preload("res://addons/atyha/atyha_peers.gd")
var peers := AtyhaPeers.new()
add_child(peers) # polls in _process
peers.opened.connect(func(code): show(code)) # host: "HK4M-7TRW"
peers.peer_connected.connect(func(p): ...) # p.reliable, p.unreliable, p.meta, p.pc
peers.failed.connect(func(error, reason): ...) # joiner
peers.host("my-game", "", "secret", {"name": "srv"}) # or:
peers.join("my-game", "HK4M-7TRW", "secret", {"player": "p1"})
AtyhaPeers.Peer.send_text(p.reliable, "hello") # text = control
AtyhaPeers.Peer.send_bytes(p.unreliable, bytes) # binary = the application's dataThe profile (spec section 9): the joiner creates reliable ({}: ordered, reliable) and unreliable ({"ordered": false, "maxRetransmits": 0}) before its offer; the host takes them from data_channel_received by label (any other label is closed). With no ICE list from either side, stun:stun.l.google.com:19302 is used (as the TS reference does). Connected peers stay in peers.peers and keep being polled until they close.
No trickle ICE on a trickle-style API
Atyha carries one complete offer and one complete answer. Godot's WebRTCPeerConnection is trickle-style: session_description_created(type, sdp) arrives before gathering, with no a=candidate lines, each candidate then comes alone through ice_candidate_created(mid, index, name), and there is no getter for the local description. So atyha_peers.gd:
- keeps the description from
session_description_created(and callsset_local_descriptionwith it: a no-op in webrtc-native, needed with the browser's WebRTC); - collects every candidate from
ice_candidate_created; - waits until
get_gathering_state() == GATHERING_STATE_COMPLETE, or 5 s; - calls
poll()once more: webrtc-native queues its signals on libdatachannel's threads and emits them frompoll(), while the gathering state is read live, so candidates found just before completion may still be in the queue; - writes the candidates in (
with_candidates()): each becomes ana=candidate:...line (a=added when missing; duplicates skipped) at the end of the media section whosea=midequals the candidate's mid (else the section at its m-line index, else the first), and each section getsa=end-of-candidateswhen gathering completed.
The host side uses webrtc-native's behaviour that set_remote_description("offer", ...) creates the answer by itself (there is no create_answer() in Godot's API).
Run the example with ATYHA_SDP=1 to print the SDP that goes out.
A trap worth knowing: lambdas connected to a WebRTCPeerConnection's signals that capture the connection's own state make a reference cycle (pc -> callable -> state -> pc), the peer connection is never freed, and webrtc-native's shutdown (rtc::Cleanup().wait()) then hangs Godot on exit. Peer.unhook() disconnects them once the channels are open or the peer closes.
Example
example/ is a project with one scene (main.tscn). With a window: a password field, Host (shows the code), a code field and Join; the log shows the echo replies. Headless, it prints the same lines as the TypeScript reference peer (ts/tools/peer.ts):
godot --headless --audio-driver Dummy --path example -- host [--app A] [--password P]
# CODE XXXX-XXXX, then PEER <meta> per joiner; echoes "x" on reliable as "echo x"
# and binary back on its channel
godot --headless --audio-driver Dummy --path example -- join <code> [--app A] [--password P]
# CONNECTED <host meta> / RELIABLE echo hi / UNRELIABLE 1,2,3, exit 0;
# or FAILED <error> <reason>, exit 2The default app is atyha-example, as in the TS peer. ATYHA_LOG=1 prints the relay log on stderr, ATYHA_SDP=1 the local SDP.
Interop (2026-11-30, real public relays, macOS arm64, Godot 4.7.2)
| host | joiner | password | result |
|---|---|---|---|
| Godot | TS peer.ts | none | CONNECTED / echo hi / 1,2,3, exit 0 |
TS peer.ts | Godot | none | CONNECTED / echo hi / 1,2,3, exit 0 |
| Godot | TS | same | exit 0 |
| TS | Godot | same | exit 0 |
TS (right-one) | Godot (wrong-one) | wrong | FAILED wrong_password wrong password, exit 2 |
Godot (right-one) | TS (wrong-one) | wrong | FAILED wrong_password wrong password, exit 2 |
| Godot | Godot | same | exit 0 |
Both peers ran on the same machine (host and server-reflexive candidates; no TURN).
Layout
CMakeLists.txt the extension (fetches godot-cpp, adds ../c)
src/ AtyhaHost, AtyhaJoiner, Atyha, registration
tools/fetch_webrtc.sh webrtc-native 1.2.2-stable -> example/addons/webrtc_native
example/project.godot the example project
example/main.tscn/.gd UI + headless CLI (echo contract)
example/addons/atyha/ atyha.gdextension, atyha_peers.gd, bin/ (built)example/addons/atyha/bin/ and example/addons/webrtc_native/ are build/download outputs (git-ignored).