@plim/transports
v0.4.0
Published
Plim transports: tiny, generic duplex-channel primitives (in-memory loopback, BroadcastChannel, reconnecting WebSocket) for syncing any JSON-safe message over any wire.
Maintainers
Readme
@plim/transports
Tiny, generic duplex-channel primitives for the Plim block editor — and anything else that needs to move JSON-safe messages over a wire.
A Transport<T> is the smallest useful abstraction over a two-way message channel:
interface Transport<T> {
send(message: T): void;
onMessage(handler: (message: T) => void): () => void;
close(): void;
}Everything in Plim that syncs — real-time collaboration, comment threads, presence — is written against this one interface, so you can swap the wire (in-process, cross-tab, WebSocket, WebRTC, your own) without touching the feature code.
Install
pnpm add @plim/transportsWhat's in the box
createMemoryTransportPair<T>()— two linked in-process transports (A↔B). Great for two-party tests and embedding.MemoryBus<T>— an in-process broadcast hub; everyconnect()returns aTransport<T>and a message from one peer is delivered to all the others (never echoed to the sender). MirrorsBroadcastChannelsemantics for N peers in one process.BroadcastChannelTransport<T>— sync across same-origin browser tabs/workers via the nativeBroadcastChannelAPI.WebSocketTransport<T>— a reconnecting JSON WebSocket client. Bufferssends while offline, flushes on (re)connect, exponential backoff, and anonStatushook. Works in the browser and in Node (pass aWebSocketimplementation).mapTransport(inner, encode, decode)— adapt aTransport<A>into aTransport<B>with a codec (e.g. wrap/compress/namespace messages).
Where to go next
- Collaboration —
@plim/collaborationsyncs over these transports. - Persistence —
@plim/storage'screateTransportAdaptersaves through one. - Sync log —
@plim/ledger.
License
See the LICENSE file in this package.
