browsermesh-transport

WebSocket, WebRTC, WebTransport, relay, and streaming adapters for BrowserMesh.
Why this exists
A mesh peer needs to move bytes to another peer, but browsers offer several incompatible ways to do that (WebRTC data channels, plain WebSockets, WebTransport) with wildly different connection-setup, NAT-traversal, and reliability characteristics -- and no single one works in every environment. browsermesh-transport exists so the rest of the mesh doesn't have to care which one is in play: MeshTransportNegotiator/TransportFactory try each candidate in a preferred order (webrtc → wsh-wt → wsh-ws) and hand back one object shaped like MeshTransport, regardless of which underlying protocol won. WebRTCMeshManager handles the harder WebRTC-specific problem of multiple independent connections per peer; GatewayNode/RouteTable and MeshRelayClient cover routing through and relaying via a gateway when a direct connection isn't possible. It depends only on browsermesh-primitives, for the shared MESH_TYPE/MESH_ERROR types its transports and streams report.
Used by
@johnhenry/browsermesh-apps treats this package as an optional peer, reaching for it lazily: webrtc-negotiator.mjs import()s WebRTCTransportAdapter at connection time, and mesh-bootstrap.mjs does the same to wire a real transport negotiator into createMeshNode(). @johnhenry/browsermesh-priority-mux is a different kind of companion -- it has no hard dependency on this package at all, but is purpose-built to wrap this package's WebSocketTransport (or anything shaped the same way) with application-level priority scheduling, since a WebSocket -- unlike the WebRTCTransport here, which gets a second data channel almost for free -- is one ordered byte stream with no way to stop a bulk transfer from blocking an urgent message behind it. See browsermesh-priority-mux's README for the full rationale.
Provenance
Extracted from a private monorepo (previously packages/browsermesh-transport), where it was manually published to npm, unscoped, as browsermesh-transport@0.1.0 (2026-07-17) with no CI ever automating that publish. This is its first release as part of the @johnhenry/browsermesh monorepo; the version restarts at 0.0.0 per family convention.
Modules
| Module |
Key Exports |
| transport |
MeshTransport, MockMeshTransport, MeshTransportNegotiator |
| websocket |
WebSocketTransport, WebRTCTransport, WebTransportTransport, NATTraversal, TransportFactory |
| wire-data |
encodeWireData, isWireNative |
| webrtc |
WebRTCPeerConnection, WebRTCMeshManager, WebRTCTransportAdapter |
| webtransport |
WebTransportBridge, WebTransportAdapterFactory |
| relay |
MeshRelayClient, MockRelayServer |
| gateway |
GatewayNode, GatewayDiscovery, RouteTable |
| streams |
MeshStream, StreamMultiplexer |
| cross-origin |
CrossOriginBridge, CrossOriginHandshake, RateLimiter |
| wsh-bridge |
MeshWshBridge (syncs a WshKeyStore with a MeshIdentityManager; the link layer does not use it) |
| wsh-frames |
encodeFrame, FrameDecoder, WshFrameError, DEFAULT_MAX_FRAME_BYTES |
wsh-links (subpath @johnhenry/browsermesh-transport/wsh) |
WshPeerLinks, WshLink, WshLinkError, createWshTransportFactory, rendezvousId |
wsh-services (subpath /wsh) |
WshServices, servicesRendezvousId, WSH_SERVICES_GRANTS (a stock wsh client may use a pod's objects and MCP tools: off by default, permitted only by the registry; needs @johnhenry/wsh 0.50 or later; see docs/design/wsh-services-on-a-pod.md) |
wsh-relay (subpath /wsh) |
WshRelay, connectWshServicesViaRelay |
wsh-pod-transport (subpath /wsh) |
WshPodTransport (a Pod TransportAdapter) |
wsh-in-process (subpath @johnhenry/browsermesh-transport/wsh-in-process, Node only) |
createInProcessPipeRelay (tests and examples) |
@johnhenry/browsermesh-transport/wsh re-exports every wsh-* module above (the two that need no wsh as well).
See Bundling for the browser.
| wisp | WispTransport |
| channel-relay | ChannelRelay |
Install
npm install @johnhenry/browsermesh-transport @johnhenry/browsermesh-primitives
Bundling for the browser
The main entry (@johnhenry/browsermesh-transport, src/index.mjs) is bundlable for a browser with
@johnhenry/wsh absent: nothing it reaches imports a @johnhenry/wsh specifier or a Node-only module,
and its only bare specifier is the @johnhenry/browsermesh-primitives peer. A test pins that
(test/main-entry-graph.test.mjs reads the import graph the way a bundler does), and the browser suite loads
the entry in a page whose import map has no wsh at all (tests/browser/transport-no-wsh.spec.mjs). So an
application that never uses wsh needs no stub, no alias and no peer: leave @johnhenry/wsh uninstalled.
What needs wsh lives behind its own subpath, imported only by code that uses it:
| You use |
Import from |
Needs @johnhenry/wsh |
wsh peer links (WshPeerLinks, WshLink, createWshTransportFactory), WshPodTransport |
@johnhenry/browsermesh-transport/wsh |
0.45 or later, on the first connect() / listen() |
wsh services over a mesh channel, the relaying pod (WshServices, WshRelay, connectWshServicesOverMesh, connectWshServicesViaRelay) |
@johnhenry/browsermesh-transport/wsh |
0.50 or later, on first use |
the in-process test relay (createInProcessPipeRelay) |
@johnhenry/browsermesh-transport/wsh-in-process (Node only) |
any wsh with the pipe relay |
@johnhenry/wsh is an optional peer ("*": any version satisfies it, so a consumer that pins an older wsh for
another reason does not get a second, nested copy). The floors above are checked when the feature is used, and a
wsh that is missing or too old fails there with a WshLinkError whose code is WSH_UNAVAILABLE. Importing
/wsh never loads wsh: its modules import it lazily with literal specifiers, so a bundler can split it out and a
page that never links over wsh never fetches it. ./wsh-in-process has only a node export condition: a
bundler for the browser cannot resolve it (it fails clearly, instead of pulling wsh's Node-only pipe relay in).
Moving off the old names (0.10 and earlier)
Up to 0.10 the main entry also re-exported WshPeerLinks, WshLink, WshLinkError, createWshTransportFactory,
rendezvousId, initiatorWins, resolveCredential, LINK_USERNAME_PREFIX, usernameForPodId, WshServices,
servicesRendezvousId, WSH_SERVICES_GRANTS, wshObjectActionFor, connectWshServicesOverMesh, WshRelay,
WSH_RELAY_PROTOCOL, connectWshServicesViaRelay and WshPodTransport. For one release (0.11) they still resolve
from the package root where the node export condition applies (Node, and a bundler built for Node), so a
Node program keeps working unchanged. Anywhere else (a bundler or import map for the browser, a vendored copy of
src/) the root no longer carries them: import them from @johnhenry/browsermesh-transport/wsh. The next
minor removes the root's node shim; change the import before then. Everything else on the main entry is
unchanged (MeshWshBridge, encodeFrame, FrameDecoder, WshFrameError, podIdToFingerprint,
fingerprintToPodId and podIdFromRawKey stay), and TransportFactory.create('wsh', { links, peerPodId }) now
builds the link through links.link(peerPodId) instead of importing it.
Usage
import { MeshTransport, WebSocketTransport, StreamMultiplexer } from '@johnhenry/browsermesh-transport';
What send() puts on the wire
RTCDataChannel.send() and WebSocket.send() accept only a string or binary
and quietly turn anything else into the text "[object Object]". Every
transport here therefore sends strings and binary (ArrayBuffer, typed arrays,
Blob) unchanged and any other value as its JSON text (encodeWireData(); it
throws a TypeError for undefined or a function rather than send the text
"undefined"). The receiving side gets a string; a consumer that wants objects
parses it (@johnhenry/browsermesh-apps' ctx.onIncomingData() does). A custom
MeshTransport should follow the same rule.
WebRTCTransport.send(data, { channel }) takes 'control' (default) or
'bulk' and falls back to control when the bulk channel is not open.
Holding a stream peer to the rules (opt-in)
StreamMultiplexer hands every inbound frame to its stream as it arrives and,
by default, checks nothing about what the peer sends: a peer that ignores the
credit it was granted, skips sequence numbers or keeps opening streams past the
limit is simply answered. Pass maxViolations (a positive whole number) and the
multiplexer enforces the rules a conforming peer already keeps, counts each
breach, and cuts the peer off once there are more of them than the budget:
const mux = new StreamMultiplexer({ maxViolations: 16 })
mux.onAbuse((reason, count) => link.close()) // called once; every stream is already cancelled
Counted (the frame is dropped): data beyond the credit granted for the stream
(grantCredits() returns credit as the reader consumes), out of sequence on an
ordered stream, larger than maxChunkSize, or beyond maxStreamSize; a credit
grant that is not a positive whole number or goes past the credit limit; a
duplicate open, an open past maxConcurrentStreams (still refused with
FLOW_CONTROL, as before) or announcing more credit than the limit; and a
frame for a stream that does not exist (still answered, as before). After the
budget is spent nothing from the peer is processed or answered. violations and
abused report the state. Receive-side only: no frame changes, so an older
peer is unaffected, and a peer that keeps the rules is never counted. Off by
default, because an existing peer that overran its credit would start losing
frames.
The credit-window flow control, bounded buffers and violation handling of the
protocol channel (browsermesh-apps' mesh-channel and protocol-stream)
are separate and unchanged. A binary frame format for a raw data channel is not
part of this package.
Binding a WebRTC connection to the pod identities (opt-in)