Docs Upgrading to 0.27

Upgrading to 0.27

Version 0.27 introduces self-certifying user identities, multi-tab presence, signed per-user and shared state, the roles() and scale() plugins, and a unified subscription API across all signaling strategies.

Wire compatibility

0.27 uses a new authenticated wire protocol (DTLS-bound ECDSA P-256 handshakes, signed state records, and v3 action frames) and is not wire-compatible with 0.26.x or earlier peers. Upgrade all browser and server peers sharing a room together.

Summary of breaking changes

  1. Property assignments become on*() methods. Replace room.onPeerJoin = fn with room.onUserJoin(({userId}) => …) (or room.onPeerJoin(({peerId, userId}) => …)), which returns an unsubscribe function.
  2. Callbacks pass context objects. Presence and media listeners receive {userId, peerId, …} rather than a positional peerId string.
  3. selfId export replaced by await room.getSelf(). Keys are generated lazily via Web Crypto without module-load side effects.
  4. onJoinError replaced by room.onError(). joinRoom(config, roomId) takes two arguments and emits typed err.code events.
  5. room.getPeers() returns metadata objects, and room.getUsers() lists unique users. getPeers() maps each peerId to {userId, connection, direct, hops, roles}.
  6. Action callbacks move to methods. Attach handlers with action.onMessage(fn), onRequest(fn), onReceive(fn), and onReceiveProgress(fn) instead of config callbacks or property assignment.

1. Joining rooms, presence, and errors

Migrate participant tracking from onPeerJoin / onPeerLeave to onUserJoin / onUserLeave to deduplicate multiple open tabs per user, and move join error handling to room.onError().

Before (≤ 0.26.x)

import {joinRoom, selfId} from 'trystero'

const room = joinRoom({appId: 'example.com/my-app'}, 'lobby', {
  onJoinError: err => console.error(err)
})

console.log('My ID:', selfId)

room.onPeerJoin = peerId => console.log(`${peerId} joined`)
room.onPeerLeave = peerId => console.log(`${peerId} left`)

After (0.27.0)

import {joinRoom} from 'trystero'

const room = joinRoom({appId: 'example.com/my-app'}, 'lobby')
const {userId, peerId} = await room.getSelf()

room.onError(err => console.error(err.code, err.message))

const offJoin = room.onUserJoin(({userId}) => console.log(`${userId} joined`))
room.onUserLeave(({userId}) => console.log(`${userId} left`))
  • Duplicate joinRoom() calls for an active (appId, roomId) now throw instead of returning the existing instance. Call await room.leave() before rejoining.
  • Explicit replay on subscribe. onUserJoin, onPeerJoin, onPeerStream, onRoleChange, and state.onChange emit only new events by default. Pass {replay: true} to replay existing entries immediately when subscribing late.
  • Custom config.peerId removed. Because peerId and userId derive from ECDSA public keys, arbitrary caller-chosen IDs are no longer accepted. Configure persistence or custom key material via config.identity.

2. Actions

makeAction(namespace, config?) now takes declarative options only ({kind, roles}), with listeners registered via .on*(fn) methods.

Before

// Tuple API (<= 0.24) or config callbacks (0.25–0.26)
const move = room.makeAction('move', {
  onMessage: (data, {peerId, metadata}) => handleMove(peerId, data)
})

move.send({x: 10, y: 20}, {target: targetPeerId})

After

const move = room.makeAction('move')

const unsub = move.onMessage((data, {userId, peerId, metadata, roles}) => {
  handleMove(userId, data)
})

// target accepts a userId (delivered to all their tabs), peerId, or array
await move.send({x: 10, y: 20}, {target: targetUserId})
  • Single active onRequest and onReceive. Registering a second handler without unsubscribing the previous one throws an error.
  • Targeted send() validation. send(data, {target}) rejects if any target is not connected, while broadcast send(data) still resolves in an empty room.
  • Typed ActionError.kind. Rejected send() and request() promises expose err.kind ('timeout' | 'disconnected' | 'aborted' | 'rejected'), and errors thrown inside onRequest propagate their message to the caller.
  • Zero-copy binary streaming. Buffers larger than 16 KiB stream directly from the provided ArrayBuffer or typed array; do not mutate the buffer until await action.send(buf) settles.

3. Audio and video

Media APIs operate on MediaStream instances. onPeerStream passes (stream, {userId, peerId, metadata}), and addStream() and replaceTrack() return Promise<void>. addTrack, removeTrack, onPeerTrack, and the metadata argument on replaceTrack have been removed; mutate tracks on your MediaStream and call await room.addStream(stream, {metadata}) again to sync changes.

Before

room.onPeerStream = (stream, peerId, metadata) => {
  attachVideo(stream, peerId, metadata)
}

await Promise.all(room.addStream(stream))

After

room.onPeerStream((stream, {userId, metadata}) => {
  attachVideo(stream, userId, metadata)
})

await room.addStream(stream)

4. Inspecting participants

Before

const peers = room.getPeers() // {[peerId]: RTCPeerConnection}
const count = Object.keys(peers).length
const pc = peers[peerId]

After

// Participant roster keyed by userId
const users = room.getUsers() // {[userId]: {peerIds, roles}}

// Per-tab connection records across direct and multi-hop peers
const peers = room.getPeers() // {[peerId]: {userId, connection, direct, hops, roles}}
const pc = peers[peerId]?.connection // RTCPeerConnection | null

5. Replacing onPeerHandshake

onPeerHandshake and handshakeTimeoutMs were removed because single-hop admission callbacks cannot run across multi-hop scale() topologies and relied on unverified signaling IDs. Use the dedicated 0.27 APIs instead:

Previous pattern 0.27 replacement
Verifying peer identity Automatic DTLS-bound userId and peerId verification (Identity)
Capping a 2- or 4-player lobby joinRoom({appId, maxUsers: 2}, roomId) + room.onError ('room-full')
Exchanging display names, profiles, or client versions room.makeUserState('profile', …), synced before onUserJoin fires
Host authority, allowlists, kicking, and banning use: [roles(…)] with grantRole(), gated state/actions, and room.ban()
Rejecting oversized or unwanted binary transfers action.onReceive(({byteLength, metadata, userId}) => …)

6. Packaging and relay helpers

  • Removed legacy subpaths and IPFS. Deprecated trystero/<strategy> subpaths, prebuilt dist/trystero-*.min.js files, and @trystero-p2p/ipfs have been removed. Import trystero (Nostr default) or @trystero-p2p/<strategy>. TypeScript 5.0+ is required for TypeScript projects.
  • Unified setRelayConnectivity(mode). relayConfig.manualReconnection, pauseRelayReconnection(), and resumeRelayReconnection() are replaced by setRelayConnectivity('offline' | 'online' | 'auto').
  • @trystero-p2p/ws-relay server. ws is now an optional peer dependency needed only on Node when importing @trystero-p2p/ws-relay/server. Passing <= 0 for maxMessagesPerSecond or maxBurstMessages throws (pass Infinity to disable rate limits).