Skip to content

Class: P2PBus

P2PBus class is an event emitter which utilizes the libp2p library to publish and subscribe to events over a peer-to-peer network. It is designed to be used in a decentralized environment where there is no central server to handle events.

Extends

Constructors

Constructor

ts
new P2PBus(
   namespace: string,
   appName?: string,
   appVersion?: string,
   psk?: string): P2PBus;

Creates a new instance of the P2PBus.

Parameters

ParameterTypeDefault valueDescription
namespacestring'p2pbus'The namespace to use for separating cross-talk between different instances of P2PBus.
appName?stringundefinedThe name of the application using P2PBus.
appVersion?stringundefinedThe version of the application using P2PBus.
psk?stringundefinedThe pre-shared key to use for encryption.

Returns

P2PBus

Overrides

ts
EventEmitter<EventEmitterEventMap>.constructor

Accessors

booted

Get Signature

ts
get booted(): boolean;

Indicates whether the P2PBus instance has been booted.

Returns

boolean


logger

Get Signature

ts
get logger(): Logger;

An event emitter specifically for logs

Returns

Logger

Methods

boot()

ts
boot(): Promise<void>;

Boots the P2PBus instance and starts listening for events.

Returns

Promise<void>


peerCount()

ts
peerCount(): Promise<number>;

Returns the count of peers we are connected to.

Returns

Promise<number>


publish()

ts
publish<K>(event: K, ...args: EventEmitterEventMap[K]): Promise<void>;

Publishes an event to the P2PBus.

Type Parameters

Type ParameterDescription
K extends "test-event"The key of the EventCallbacks interface which represents the published event.

Parameters

ParameterTypeDescription
eventKThe name of the event to publish.
...argsEventEmitterEventMap[K]The arguments to pass to the event callback.

Returns

Promise<void>

Throws

P2PBusNotBootedError if the P2PBus instance has not been booted.

Throws

V8UnserializableError if the event could not be serialized correctly.


shutdown()

ts
shutdown(): Promise<void>;

Shuts down the P2PBus instance and stops listening for events.

Returns

Promise<void>


waitForPeer()

ts
waitForPeer(signal?: AbortSignal): Promise<void>;

Awaits for at least 1 peer to be discovered

Parameters

ParameterType
signal?AbortSignal

Returns

Promise<void>