✨🤝✨ Trystero
Instant peer-to-peer multiplayer for the web — no server required.
👉 Try it live on trystero.dev 👈
Making your app work with multiple users usually means a detour into setting up
servers, auth, billing, databases, etc. — all so two browsers can say hello.
Trystero skips the backend entirely. Import the library, join a room by
name, and browsers discover each other automatically and connect directly over
fast, end-to-end encrypted WebRTC. Out of the box, you get verified user
identities, synced user and room state, host permissions, RPC, chunked file
transfers, and live audio/video. The web was meant to be decentralized and
Trystero makes it easy to build apps where data flows without middlemen.
Quickstart
Install Trystero from npm:
npm i trystero
(Or skip the build step and import from a script tag)
<script type="module">
import {joinRoom} from 'https://esm.run/trystero'
</script>
Join a room with a unique app ID and room name and start syncing state. Here’s a
complete multiplayer cursor canvas in a dozen lines:
import {joinRoom} from 'trystero'
const room = joinRoom({appId: 'pixel-party.example.com'}, 'studio-49')
const cursor = room.makeUserState('cursor', {x: 0, y: 0})
window.addEventListener('pointermove', e =>
cursor.set({x: e.clientX, y: e.clientY})
)
cursor.onChange((pos, {userId, self, removed}) => {
if (self) {
return
}
if (removed) {
removeCursor(userId)
} else {
drawCursor(userId, pos)
}
})
Open your page in two browsers anywhere in the world and watch the cursors move.
Local testing tip: Tabs in the same browser profile share your userId
automatically. Open a Private/Incognito window (or pass identity: 'none' in
the room config) when simulating a second person on one machine.
What you can build with it
1. Synced user & room state
Most multiplayer apps need two kinds of live state: per-user state that each
participant owns (names, avatars, ready flags, cursors) and shared room
state for the group (a game board, round timer, or watch-party queue).
Trystero builds both right onto room. Updates apply immediately locally,
coalesce rapid writes on the wire, verify who wrote them, and sync automatically
to late joiners — even if the person who last updated shared state has already
closed their laptop.
const profile = room.makeUserState('profile', {name: 'Anon', ready: false})
profile.set({name: 'Oedipa', ready: true})
console.log(profile.getAll())
const track = room.makeState('track', {url: 'chill-beat.mp3', paused: true})
track.onChange((next, {userId}) => {
syncAudioPlayer(next)
console.log(`Updated by ${userId}`)
})
await track.set(prev => ({...prev, paused: false}))
Using React? Both makeUserState and makeState plug straight into
useSyncExternalStore — no useEffect glue or manual join/leave maps:
import {useSyncExternalStore} from 'react'
function Lobby() {
const players = useSyncExternalStore(profile.onChange, profile.getAll)
const nowPlaying = useSyncExternalStore(track.onChange, track.get)
// render directly from `players` and `nowPlaying`
}
👉 Shared & Per-User State guide →
2. Unforgeable user identity, passwords & room caps
Every participant gets a persistent, cryptographically verified userId out of
the box — no login screens or auth servers required. If someone refreshes the
page or opens your app in two tabs at once, Trystero recognizes them as the same
user automatically. (Want strictly throwaway IDs or keys loaded from your own
backend? One config option switches modes.)
Combine password and maxUsers to spin up private, seat-capped matches like a
2-player chess table or a 4-player co-op lobby:
const room = joinRoom(
{
appId: 'chess.example.com',
password: 'knight-takes-f7-secret',
maxUsers: 2
},
'table-7'
)
const {userId} = await room.getSelf()
room.onUserJoin(({userId}) => console.log(`Opponent ${userId} sat down`))
room.onUserLeave(({userId}) => console.log(`Opponent ${userId} left`))
room.onError(err => {
if (err.code === 'room-full' && Object.keys(room.getPeers()).length === 0) {
showBanner('Sorry, both seats at this table are taken!')
}
})
👉 Identity, Presence & Room Access guide →
3. Serverless hosts, custom roles & moderation
In a typical P2P app, everyone is equal — which gets awkward the moment someone
opens DevTools in your game and awards themselves a million points.
Import the tree-shakeable roles() plugin to create rooms with mathematically
verified admins, custom roles (like 'dj' or 'moderator'), authoritative
state that only authorized roles can edit, and peer banning — still with zero
backend:
import {joinRoom, roles} from 'trystero'
const room = joinRoom(
{appId: 'oedipa/friday-trivia', use: [roles({selfAsAdmin: true})]},
'quiz-night'
)
const {roomId} = await room.getSelf()
const game = room.makeState('game', {round: 1, prompt: 'Ready?'})
await game.set({round: 2, prompt: 'Name the moons of Mars'})
await room.grantRole(friendId, 'cohost')
await room.ban(grieferId, {reason: 'spoiling answers'})
👉 Admins, Roles & Moderation guide →
4. Events, RPC & direct P2P file drop
For ephemeral events, request/response calls, and binary transfers, create typed
channels with room.makeAction(). Pass strings, JSON objects, Blobs, Files,
or TypedArrays — Trystero handles serialization, chunking, backpressure,
metadata headers, and transfer progress automatically, with zero cloud storage
bills:
const drop = room.makeAction('file-drop')
drop.onReceive(({byteLength}) => byteLength <= 100 * 1024 ** 2)
drop.onReceiveProgress((pct, {metadata}) => showDownloadBar(metadata.name, pct))
drop.onMessage((bytes, {userId, metadata}) => saveFile(bytes, metadata.name))
await drop.send(file, {
metadata: {name: file.name},
onProgress: pct => showUploadBar(pct)
})
const roll = room.makeAction('roll-check', {kind: 'request'})
roll.onRequest(() => Math.ceil(Math.random() * 20))
const result = await roll.request(null, {target: friendId, timeoutMs: 2000})
👉 Actions, RPC & File Transfers guide →
5. Voice, video & screen sharing
Drop live audio, webcam video, screen captures, or Web Audio streams into any
room, and tag streams with metadata so peers can tell a facecam from a screen
share:
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
video: true
})
await room.addStream(stream, {metadata: {type: 'webcam'}})
room.onUserJoin(({userId}) =>
room.addStream(stream, {target: userId, metadata: {type: 'webcam'}})
)
room.onPeerStream((remoteStream, {userId, metadata}) =>
attachVideo(userId, remoteStream, metadata.type)
)
👉 Audio, Video & Screen Sharing guide →
6. Scale to large numbers of peers
Standard WebRTC connects every browser directly to every other browser, which is
ideal for smaller hangouts and matches. Building a 100-person collaborative
canvas or a crowded party lobby? Opt into the scale() plugin:
import {joinRoom, scale} from 'trystero'
const room = joinRoom({appId: 'canvas.example.com', use: [scale()]}, 'plaza')
Small rooms stay in a fast direct mesh with zero extra overhead. As more people
pile in, Trystero automatically forms a multi-hop gossip network so no single
browser gets overwhelmed — while your state, action, and media code stays
exactly the same.
👉 Scaling Large Rooms guide →
7. Run headless peers in Node, Bun & Deno
Trystero runs outside the browser too. Drop a lightweight peer onto a Raspberry
Pi, build a game bot, or run a silent passive: true peer on a tiny box to keep
shared room state warm even when every browser tab closes:
import {joinRoom} from 'trystero'
import {RTCPeerConnection} from 'werift'
const room = joinRoom(
{appId: 'oedipa/synth-jam', rtcPolyfill: RTCPeerConnection, passive: true},
'room-1'
)
room.makeState('pattern', {bpm: 120, steps: []})
👉 Running in Node, Bun & Deno guide →
How does Trystero connect users with no backend?
WebRTC lets browsers talk directly to each other, peer-to-peer, without routing
traffic through a central server. The one catch with WebRTC has always been
signaling: two browsers need a brief introduction to swap connection info
before they can link up.
Trystero handles that introduction automatically out of the box using
decentralized public relays — nothing to deploy or configure. Once peers
connect, your app’s data never touches a relay: state, messages, files, and
media flow straight from browser to browser, end-to-end encrypted.
Prefer to run your own signaling setup with Nostr, MQTT, BitTorrent, Supabase,
Firebase, or a tiny self-hosted WebSocket relay? You can swap strategies with a
one-line import change — see the
Signaling & Relays guide.
Peek under the hood: how serverless discovery and security work
- Encrypted signaling: Connection offers (SDPs) are encrypted with
AES-256-GCM before touching any relay. Adding a room
password derives the
key via WebCrypto PBKDF2-SHA256 (600,000 iterations) so outsiders and relay
operators can’t read handshakes or join the room.
- Self-certifying identities: Every user (
userId) and browser tab
(peerId) is derived from a WebCrypto ECDSA P-256 public keypair. During
connection setup, peers verify each other’s keys against the WebRTC DTLS
certificate fingerprint
(RFC 8827), preventing spoofing
and man-in-the-middle relays.
- Signed state & roles: Updates to
makeUserState, makeState, and
roles() carry cryptographic signatures and Hybrid Logical Clock timestamps,
allowing peers (including late joiners) to verify who wrote what without a
central server.
Documentation
Dive deeper into any feature or browse the full API:
| Guide |
What’s inside |
| Shared & Per-User State |
makeUserState, makeState, validation rules, late-joiner sync, and React (useSyncExternalStore) |
| Identity, Presence & Room Access |
userId vs peerId, multi-tab presence, custom identity keys, room passwords, maxUsers, and onError |
| Admins, Roles & Moderation |
roles(), self-certifying rooms, fixed admins, custom roles, authoritative state, and banning |
| Actions, RPC & File Transfers |
makeAction, send, request & requestMany, TypeScript types, binary metadata, progress bars, and onReceive |
| Audio, Video & Screen Sharing |
addStream, onPeerStream, removeStream, camera/screen switching (replaceTrack), and stream metadata |
| Scaling Large Rooms |
Multi-hop routing with scale(), direct media/file promotion, and getTopology() |
| Signaling & Relays |
Default zero-config relays, Supabase, Firebase, MQTT, BitTorrent, self-hosted ws-relay, custom strategies, and bundle sizes |
| Running in Node, Bun & Deno |
Headless peers, Raspberry Pi / IoT setups, and passive state backup peers |
| NAT Traversal & Troubleshooting |
Configuring TURN servers, diagnosing strict firewalls, and ping() |
| Full API Reference |
Complete function signatures, TypeScript types, config options, and error codes |
| Upgrading to 0.27.0 |
Architectural overview, breaking changes, and before/after migration examples |
Trystero by Dan Motzenbecker
评论