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
new P2PBus(
namespace: string,
appName?: string,
appVersion?: string,
psk?: string): P2PBus;Creates a new instance of the P2PBus.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
namespace | string | 'p2pbus' | The namespace to use for separating cross-talk between different instances of P2PBus. |
appName? | string | undefined | The name of the application using P2PBus. |
appVersion? | string | undefined | The version of the application using P2PBus. |
psk? | string | undefined | The pre-shared key to use for encryption. |
Returns
P2PBus
Overrides
EventEmitter<EventEmitterEventMap>.constructorAccessors
booted
Get Signature
get booted(): boolean;Indicates whether the P2PBus instance has been booted.
Returns
boolean
logger
Get Signature
get logger(): Logger;An event emitter specifically for logs
Returns
Methods
boot()
boot(): Promise<void>;Boots the P2PBus instance and starts listening for events.
Returns
Promise<void>
peerCount()
peerCount(): Promise<number>;Returns the count of peers we are connected to.
Returns
Promise<number>
publish()
publish<K>(event: K, ...args: EventEmitterEventMap[K]): Promise<void>;Publishes an event to the P2PBus.
Type Parameters
| Type Parameter | Description |
|---|---|
K extends "test-event" | The key of the EventCallbacks interface which represents the published event. |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | K | The name of the event to publish. |
...args | EventEmitterEventMap[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()
shutdown(): Promise<void>;Shuts down the P2PBus instance and stops listening for events.
Returns
Promise<void>
waitForPeer()
waitForPeer(signal?: AbortSignal): Promise<void>;Awaits for at least 1 peer to be discovered
Parameters
| Parameter | Type |
|---|---|
signal? | AbortSignal |
Returns
Promise<void>