Getting started
Every implementation has the same two sides. The host opens a room and answers every joiner whose code (and password) is right; the joiner turns a code into an open connection. The examples below use the recommended DataChannel profile: reliable (ordered, reliable) and unreliable (unordered, no retransmits), both made by the joiner before its offer.
Pick an application id (app) of your own, such as "my-game": rooms of another application are never seen.
TypeScript
import { hostPeers, joinPeer, displayCode } from 'atyha';
// the host
const host = hostPeers({
app: 'my-game',
password: '', // optional; any length
onOpen: () => console.log('code', displayCode(host.code)),
onPeer: (peer) => {
peer.reliable.onmessage = (e) => peer.reliable.send('echo ' + e.data);
},
});
// the joiner
const peer = await joinPeer({ app: 'my-game', code: 'hk4m 7trw', password: '' });
peer.reliable.send('hi');In Node pass RTCPeerConnection from node-datachannel/polyfill. Errors are JoinError with a code: bad_code, no_relay, no_room, wrong_password, rejected, no_answer, cancelled.
C
The C library is signalling only and never blocks: call the poll every frame.
#include <atyha.h>
#include <atyha_libdatachannel.h>
atyha_ws_backend* ws = atyha_libdatachannel_backend_create(NULL);
atyha_join_config jc = { .app = "my-game", .code = "HK4M-7TRW", .password = "" };
jc.transport.ws = ws;
atyha_joiner* j = atyha_joiner_create(&jc);
atyha_event ev;
while (atyha_joiner_poll(j, atyha_now_ms(), &ev)) {
if (ev.type == ATYHA_EV_NEED_OFFER) {
/* make the peer connection with ev.ice (ev.ice_count servers),
wait for ICE gathering, then: */
atyha_joiner_set_offer(j, my_offer_sdp);
} else if (ev.type == ATYHA_EV_ANSWER) {
/* set ev.sdp as the remote description */
} else if (ev.type == ATYHA_EV_ERROR) {
printf("failed: %s\n", atyha_error_name(ev.error));
}
}The host side is atyha_host_create, atyha_host_poll (events ATYHA_EV_OPEN and ATYHA_EV_OFFER) and atyha_host_answer / atyha_host_reject, which may be called on any later tick.
Go
import (
atyha "github.com/ngspades/atyha/go"
"github.com/ngspades/atyha/go/rtc"
)
// the host
host, err := rtc.HostPeers(rtc.HostPeersOptions{
HostOptions: atyha.HostOptions{App: "my-game", Password: ""},
OnPeer: func(p *rtc.Peer) { /* p.Reliable, p.Unreliable */ },
})
<-host.Opened()
fmt.Println("code:", atyha.DisplayCode(host.Code()))
// the joiner
peer, err := rtc.JoinPeer(ctx, rtc.JoinPeerOptions{JoinOptions: atyha.JoinOptions{
App: "my-game", Code: "hk4m 7trw", Password: "",
}})
if errors.Is(err, atyha.ErrWrongPassword) { /* ask for it */ }
peer.Reliable.SendText("hi")C# and Unity
using Atyha;
var host = new Host(new HostOptions {
App = "my-game",
OnOpen = () => Debug.Log("code " + host.DisplayCode),
OnOffer = async offer => OfferReply.Answer(await MakeAnswer(offer.Sdp)),
});
try {
var joined = await Joiner.JoinAsync(new JoinOptions {
App = "my-game", Code = "HK4M-7TRW", Password = "",
MakeOffer = async (ice, hostMeta) => await MakeOffer(ice),
});
// joined.Sdp is the host's answer
} catch (JoinException e) when (e.Code == JoinErrorCode.WrongPassword) { }In Unity, UnityPeers.HostPeers and UnityPeers.JoinPeerAsync do the WebRTC part with com.unity.webrtc.
Godot
var joiner := AtyhaJoiner.new()
joiner.need_offer.connect(_on_need_offer) # make the peer, then joiner.set_offer(sdp)
joiner.answered.connect(_on_answered) # set the remote description
joiner.failed.connect(func(error, reason): print(error, " ", reason))
joiner.join("my-game", "HK4M-7TRW", "", {})
func _process(_delta):
joiner.poll()addons/atyha/atyha_peers.gd wraps both sides with Godot's WebRTCPeerConnection; see Godot.
Try it
The repository's reference peer hosts or joins from a terminal, and every implementation's example prints the same lines, so any two can be tried together:
cd ts && pnpm install
node tools/peer.ts host --password 123 # prints CODE XXXX-XXXX
node tools/peer.ts join XXXX-XXXX --password 123 # CONNECTED, RELIABLE echo hi