Class DandelionMesh<T>

DandelionMesh — a fault-tolerant P2P mesh network for browser applications.

Combines three layers:

  • Transport (default: PeerJS) — handles peer-to-peer WebRTC connections.
  • Crypto — RSA-OAEP + AES-256-GCM hybrid encryption for private messages.
  • Raft consensus — leader election and totally-ordered log replication.

All application messages (public broadcasts and encrypted private messages) flow through the Raft log, guaranteeing every peer sees the same events in the same order. Non-leader peers forward proposals to the current leader.

Events

Event Payload When
ready localPeerId: string Transport is open and Raft has started
message MeshMessage<T>, replay: boolean A committed log entry is delivered
peersChanged peers: string[] A peer connects or disconnects
leaderChanged leaderId: string | null Raft leader changes
error Error Transport-level error

Example

const transport = new PeerJSTransport({ peerId: 'alice' });
const mesh = new DandelionMesh<GameAction>(transport, {
bootstrapPeers: ['bob', 'charlie'],
});

mesh.on('ready', (id) => console.log('My ID:', id));
mesh.on('message', (msg) => {
if (msg.type === 'public') console.log(msg.sender, msg.data);
if (msg.type === 'private') console.log('secret from', msg.sender);
});

await mesh.sendPublic({ action: 'bet', amount: 100 });
await mesh.sendPrivate('bob', { cards: ['Ah', 'Kd'] });

Example

Providing a pre-generated crypto key bundle

const bundle = await generateKeyBundle(2048);
const mesh = new DandelionMesh(transport, { cryptoKeyBundle: bundle });

Type Parameters

  • T = unknown

    The application-level payload type for messages.

Hierarchy

  • DandelionMesh

Constructors

Properties

bootstrapPeers: string[]
cryptoBundle: CryptoKeyBundle = null
cryptoReady: Promise<CryptoKeyBundle>
lastAppliedIndex: number = 0
listeners: ListenerMap<T> = ...
localPeerId: string
modulusLength: number
peerPublicKeys: Map<string, Promise<CryptoKey>> = ...
pendingKeyWaiters: Map<string, ((key) => void)[]> = ...
raftLog: RaftLog<MeshLogCommand<T>>
raftNode: RaftNode<MeshLogCommand<T>> = null
raftOptions: RaftNodeOptions
transport: Transport

Accessors

Methods

  • Parameters

    • peerId: string

    Returns Promise<CryptoKey>

  • Parameters

    • cmd: EncryptedPrivateMessage
    • isReplay: boolean

    Returns Promise<void>

  • Parameters

    • fromPeerId: string
    • data: unknown

    Returns void

  • Send an encrypted private message through the Raft cluster

    Parameters

    • recipientPeerId: string
    • data: T

    Returns Promise<boolean>

  • Send a public (broadcast) message through the Raft cluster

    Parameters

    • data: T

    Returns Promise<boolean>

  • Parameters

    • channel: "raft" | "control"
    • payload: unknown

    Returns WireMessage

Generated using TypeDoc