@codefusion-cc/realtime-p2p
v0.1.1
Published
Peer-to-peer data for realtime apps: a WebRTC mesh between the connections of one channel, negotiated through it, carrying messages and content-addressed files checked against their SHA-256
Maintainers
Readme
@codefusion-cc/realtime-p2p
Peer-to-peer data for realtime apps: a WebRTC mesh between the connections of one channel. The channel only passes their signals; messages and files go straight between the browsers and never through the server. It is how an app keeps content off its servers (Peerly's free tier) and still syncs it between whoever is online.
npm install @codefusion-cc/realtime-p2pimport { openDurableChannel } from '@codefusion-cc/realtime/browser'
import { createDataMesh } from '@codefusion-cc/realtime-p2p'
const channel = await openDurableChannel({ endpointPrefix: '/api/workspaces/', authorize, encryptionSecret, namespace: 'example' })
const mesh = createDataMesh({
channel, // any MeshChannel: connections, joins and leaves, an event sent `to` one
iceServers: [{ urls: 'stun:stun.cloudflare.com:3478' }], // add TURN when relays are allowed
accept: (connectionId, { userId }) => !blocked.has(userId),
onMessage: (message, from) => apply(message, from.userId),
provide: async hash => files.get(hash) ?? null, // what this device can share, by SHA-256
})
mesh.send({ type: 'hello' }) // to every open peer, or { to: connectionId }
const file = await mesh.fetchBlob(sha256Hex, { onProgress }) // from the first peer that has it, checked- Each pair negotiates with WebRTC's perfect negotiation through the channel event
signalEvent(p2p, which the channel must allow); the channel encrypts signals when it encrypts its events, so the server cannot swap the DTLS fingerprints in them. A link that does not connect restarts ICE once, then counts as failed:refresh()tries again, andsetIceServers(servers, true)restarts with relays once the app allows them. - Blobs are content-addressed. A peer that has one streams it in 16 KiB chunks with backpressure; the bytes count
only once they hash to the id asked for, so a peer can neither poison a file nor replace it. A peer serves at most
four at once;
maxBlobBytes(100 MB) bounds what is served and accepted. - A peer that is not online cannot be reached: the app keeps what it must deliver later and sends it when the peer joins.
PeerLink is the single connection underneath, for an app that negotiates its own WebRTC through another channel
(@codefusion-cc/realtime-media's calls do): connectMs (20 s) before ICE restarts once, restartMs (as long) before
the link fails, disconnectedMs (8 s) a connection may stay down before it restarts. While a connection is down or
restarting the link says connecting; one that connects again gets its own restart for the next drop. An offer of
its own that the browser refuses fails the link, which stays failed while the connection is up: nothing would offer
again. For media, setup(pc, forOffer) readies each connection the link makes and beforeAnswer(pc) puts the
app's tracks on the transceivers an offer made; answerOnNewConnection answers a first offer that crossed this
side's own on a new connection rather than roll it back (libwebrtc keeps a rolled-back offer's header extension ids
and can refuse the next one), and forOffer starts a link that waits for the other side's offer and offers itself
at the deadline if it never comes. A link with either makes its data channels in setup, so a new connection gets
them too; createDataChannel throws there.
When both sides offer at once on a new connection, the polite side takes the other's offer once its own has gathered a
candidate (a second at most): Chromium never gathers on a connection whose first offer it rolled back before its
network list came.
| Entry | Runs in | What it has |
| --- | --- | --- |
| @codefusion-cc/realtime-p2p | page | createDataMesh, PeerLink |
| @codefusion-cc/realtime-p2p/protocol | both | parsePeerSignal and its limits: a server that passes signals on checks them as the links do |
| @codefusion-cc/realtime-p2p/testing | tests | FakePeerConnection: negotiation without WebRTC, for tests in Node |
