Get started
PeerConnect gives two applications a private, reliable channel through a relay neither has to trust. It is offered as an API: this guide covers the ideas you need, getting access, what a client implements, and running your own relay. The API reference and the specification have every detail.
Concepts
- Room
- The relationship between two peers. It is defined by a random room secret and the two peers’ identity keys — not by anything stored on a relay.
- Inviter and joiner
- The inviter creates the room and hands out an invite; the joiner accepts it. A room has exactly these two peers.
- Invite
- The relay URL, the room secret and the inviter’s public key, as one string such as
wss://relay.peerconnect.dev#…. Show it as a QR code or a link. Until pairing completes, whoever uses it first becomes the joiner, so pass it on privately. - Pairing
- On first contact the inviter pins the joiner’s identity and both peers move to a pair ID derived from their two identities. From then on the invite leads nowhere.
- Room record
- Everything a peer needs to reconnect: the room secret, its identity key, the peer’s key once pinned, and where the two meet (§3.4). It contains secrets, so store it like a password. Save it when the room is created or joined, when pairing completes, and when the room starts closing.
- Connection
- A peer’s live link to the room through the relay. A client reconnects on its own, with backoff, and is offline (connecting or waiting to retry), waiting (on the relay, the peer is not), connected (end-to-end session established) or closed.
- Delivery
- While both peers keep running, every message is delivered exactly once and in order, however often connections drop. Messages sent while the peer is away are kept by the sender and delivered once both are connected.
Get access
The public relay, wss://relay.peerconnect.dev, requires a token on every connection, so that it knows
which application uses it and can stop abuse; it learns nothing more from it. Each peer brings its
own token, which is not part of the room or the invite. There are three ways to get one:
- The public trial, for trying PeerConnect:
POST /api/trialanswers a token that lasts two hours, without an account. - Your users connect your app. Register it on the developers page (sign in with GitHub) for an app ID and an app
secret. People open
peerconnect.dev/connectfrom your app, sign in, and come back with a code that your app exchanges for their token (OAuth 2.0 with PKCE). - Your server issues the tokens, for apps whose users have no reason to see PeerConnect: it asks for one per user with the app secret.
# Try it: a trial token, no account (two hours)
curl -X POST https://peerconnect.dev/api/trial
# → {"token": "eyJ…", "expires_at": …, "relay_url": "wss://relay.peerconnect.dev"}
# Your app’s server: a token per user of yours, with the app secret
curl https://peerconnect.dev/api/apps/app_…/tokens \
-H "authorization: Bearer $PEERCONNECT_APP_SECRET" \
-H "content-type: application/json" \
-d '{"user": "user-42"}'
# Then each peer connects with its own token
# wss://relay.peerconnect.dev/v1/<room id>?token=<token>A peer connects to wss://relay.peerconnect.dev/v1/<room ID>?token=<token>. If the relay
closes the connection with 4403, the token is missing, invalid or revoked: get a new one before
connecting again. With 4429, wait at least a minute. Every request, answer and error is in the API reference. Your own relay needs no token unless you give it a secret (PEERCONNECT_TOKEN_SECRET).
Build a client
There is no SDK to install: your application implements the peer side of the specification, with any WebSocket library and the usual primitives (Ed25519, X25519, ChaCha20-Poly1305, SHA-256). What it takes:
- Rooms and invites (§3): the room secret, the room ID derived from it, the invite, and the record each peer keeps.
- Joining the relay (§4): the endpoint, the HELLO challenge and the signed JOIN, presence, and the keepalive.
- The handshake and pairing (§5.1–5.2):
Noise_IKpsk1_25519_ChaChaPoly_SHA256, the inviter pinning the joiner, the move to the pair ID. - The reliable stream (§5.3–5.5): numbered messages, acknowledgements, resending after a reconnection, and ending a room.
- The connection lifecycle (§6) and the timers (§7).
Check every derivation against the test vectors before talking to
anyone. The repository's own implementations are there to read and to test against, not to
install: the Rust core, a state
machine without I/O (src/peer.rs), and the JavaScript one, the same core in
WebAssembly driven by about 200 lines of JavaScript. For an AI agent writing the client, /llms.txt has the essentials and /llms-full.txt everything.
Run a relay
A relay forwards frames between the two seats of a room and stores nothing. It checks that each
connection holds the room’s key without learning it, so it needs no accounts or configuration.
Anyone may use the public relay wss://relay.peerconnect.dev for testing; for anything else, run your own.
Cloudflare Workers
Each room ID gets its own Durable Object, using the WebSocket Hibernation API, so idle rooms cost
nothing and a misbehaving room can only exhaust itself. Add a custom domain in the Cloudflare
dashboard to serve it as wss://relay.your-domain.
git clone https://github.com/atshelchin/peerconnect && cd peerconnect
cargo install worker-build
cd crates/peerconnect-worker
npx wrangler deploy # prints your wss:// URLDocker
The image holds a single static binary. Run it behind any proxy that terminates TLS, or use the bundled Compose file, which adds Caddy for automatic HTTPS.
git clone https://github.com/atshelchin/peerconnect && cd peerconnect
docker build -t peerconnect-relay .
docker run -p 8080:8080 peerconnect-relay # ws://localhost:8080
# With automatic HTTPS (Caddy), on a host that DOMAIN points to:
DOMAIN=relay.example.com docker compose -f deploy/docker-compose.yml up -dBinary
cargo install peerconnect-relay
peerconnect-relay # listens on 0.0.0.0:8080
# Optional: require tokens, and mint one
export PEERCONNECT_TOKEN_SECRET=$(openssl rand -hex 32)
peerconnect-relay token my-app --ttl 30d| Variable | Default | Meaning |
|---|---|---|
PEERCONNECT_LISTEN | 0.0.0.0:8080 | Address to listen on. |
PEERCONNECT_MAX_CONNECTIONS | 10000 | Concurrent WebSockets. |
PEERCONNECT_MAX_PER_ADDRESS | 256 | Concurrent WebSockets per client address (IPv6 counted per /64). |
PEERCONNECT_TRUST_PROXY | off | Set to 1 behind a proxy that appends the client address to X-Forwarded-For. |
Every relay answers GET /healthz for health checks.
Limits
| What | Limit |
|---|---|
| Peers per room | 2 |
| Message size | 16 MiB, sent in frames of up to 64 KiB |
| Unacknowledged data in flight | 4 MiB (a single larger message may exceed it) |
| Messages held for the peer | 64 MiB in the reference implementations; beyond that a sender refuses or waits |
| Keepalive | a ping every 25 s; a connection that hears nothing for 60 s is re-established |
Security checklist
- Store room records like passwords — in the Keychain, the Android Keystore or equivalent encrypted storage. Whoever has a record can act as that peer.
- Save the joiner’s record before connecting: it holds the identity the inviter will pin.
- Send invites over a private channel. Until pairing completes, an invite is a bearer credential.
- Where pairing the wrong device would be costly, let users compare a short code derived from both identity keys (the two identity public keys) — the demo shows one.
- Delete a room’s record once the room is closed.
Limitations
- The queue lives in the sender’s memory. Messages survive disconnections and relay restarts, but not the sending process: keep your own copy until the peer has acknowledged it if you need delivery across restarts. A receiver that restarts may get again what it received in its last ~50 ms.
- Ending a room needs both peers online once more. A closing peer keeps trying until the other peer acknowledges; a peer that never returns is never told.
- Before pairing, the invite is a bearer credential. Whoever uses it first pairs. The race is visible — the rightful joiner never connects — but not prevented.
- One relay per room. If it is down, peers wait. They can agree on another relay URL; there is no automatic failover.
- The Cloudflare relay enforces the idle timeout lazily and cannot apply backpressure; the 4 MiB window bounds honest peers.
- Not independently audited. The design has had several rounds of adversarial review and its test vectors are reproduced by an independent implementation; neither replaces an external cryptographic audit.
The full protocol, with its threat model, is in the specification.