Skip to content

Class: Swarm<Events> ​

The class that provides peer communication between browser tabs and service workers

Extends ​

  • TypedEventEmitter<Events>

Type Parameters ​

Type ParameterDefault typeDescription
Events extends TypedEventMapTypedEventMapThe map of events and their arguments that can be emitted and listened to.

Accessors ​

id ​

Get Signature ​

ts
get id(): string;

The ID of the current instance.

Returns ​

string


leader ​

Get Signature ​

ts
get leader(): boolean;

Whether this instance is the leader or not.

Returns ​

boolean

Methods ​

collect() ​

ts
collect<R, E>(event: E, ...args: Events[E]): Promise<R[]>;

Collect responses from all peers for a specific event.

Type Parameters ​

Type ParameterDefault typeDescription
R-The type of the response.
E extends string | number | symbolkeyof EventsThe name of the event to collect responses for.

Parameters ​

ParameterTypeDescription
eventEThe name of the event to collect responses for.
...argsEvents[E]The arguments which will be passed to the event handler.

Returns ​

Promise<R[]>

A promise that resolves with an array of responses from all peers.


emit() ​

ts
emit<E>(event: E, ...args: Events[E]): this;

Emit an event to all connected peers.

Type Parameters ​

Type ParameterDescription
E extends string | number | symbolThe name of the event to emit.

Parameters ​

ParameterTypeDescription
eventEThe name of the event to emit.
...argsEvents[E]The arguments to pass to the event listeners.

Returns ​

this

Overrides ​

ts
TypedEventEmitter.emit;

off() ​

ts
off<E>(event: E, listener?: (...args: Events[E]) => void): this;

Remove a listener.

Type Parameters ​

Type Parameter
E extends string | number | symbol

Parameters ​

ParameterType
eventE
listener?(...args: Events[E]) => void

Returns ​

this

Inherited from ​

ts
TypedEventEmitter.off;

offCollect() ​

ts
offCollect<E>(event: E): Swarm<Events>;

De-register a handler for a specific event which will respond to incoming messages which require a general response

Type Parameters ​

Type ParameterDescription
E extends string | number | symbolThe name of the event to listen for.

Parameters ​

ParameterTypeDescription
eventEThe name of the event to listen for.

Returns ​

Swarm<Events>


offLeadershipChange() ​

ts
offLeadershipChange(cb: (is: boolean) => void): Swarm<Events>;

Remove the leadership change event listener.

Parameters ​

ParameterTypeDescription
cb(is: boolean) => voidThe callback function to be removed from the leadership change event.

Returns ​

Swarm<Events>


offRequest() ​

ts
offRequest<E>(event: E): Swarm<Events>;

De-register a handler for a specific event which will respond to incoming messages which require a response from the leader

Type Parameters ​

Type ParameterDescription
E extends string | number | symbolThe name of the event to listen for.

Parameters ​

ParameterTypeDescription
eventEThe name of the event to listen for.

Returns ​

Swarm<Events>


on() ​

ts
on<E>(
   event: E,
   listener: (...args: Events[E]) => void,
   ctx?: any): this;

Subscribe to an event with a typed listener.

Type Parameters ​

Type Parameter
E extends string | number | symbol

Parameters ​

ParameterType
eventE
listener(...args: Events[E]) => void
ctx?any

Returns ​

this

Inherited from ​

ts
TypedEventEmitter.on;

once() ​

ts
once<E>(
   event: E,
   listener: (...args: Events[E]) => void,
   ctx?: any): this;

Subscribe once to an event.

Type Parameters ​

Type Parameter
E extends string | number | symbol

Parameters ​

ParameterType
eventE
listener(...args: Events[E]) => void
ctx?any

Returns ​

this

Inherited from ​

ts
TypedEventEmitter.once;

onceLeadershipChange() ​

ts
onceLeadershipChange(cb: (is: boolean) => void): Swarm<Events>;

Subscribe to the next occurance of a leadership change event.

Parameters ​

ParameterTypeDescription
cb(is: boolean) => voidThe callback function to be called when the leadership status changes.

Returns ​

Swarm<Events>


onCollect() ​

ts
onCollect<E>(event: E, handler: (...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]>): Swarm<Events>;

Register a handler for a specific event which will respond to incoming messages which require a general response

Type Parameters ​

Type ParameterDescription
E extends string | number | symbolThe name of the event to listen for.

Parameters ​

ParameterTypeDescription
eventEThe name of the event to listen for.
handler(...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]>The function to call when the event is emitted.

Returns ​

Swarm<Events>


onLeadershipChange() ​

ts
onLeadershipChange(cb: (is: boolean) => void): Swarm<Events>;

Subscribe to the leadership change event.

Parameters ​

ParameterTypeDescription
cb(is: boolean) => voidThe callback function to be called when the leadership status changes.

Returns ​

Swarm<Events>


onRequest() ​

ts
onRequest<E>(event: E, handler: (...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]>): Swarm<Events>;

Register a handler for a specific event which will respond to incoming messages which require a response from the leader

Type Parameters ​

Type ParameterDescription
E extends string | number | symbolThe name of the event to listen for.

Parameters ​

ParameterTypeDescription
eventEThe name of the event to listen for.
handler(...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]>The function to call when the event is emitted.

Returns ​

Swarm<Events>


request() ​

ts
request<R, E>(event: E, ...args: Events[E]): Promise<R>;

Request feedback from the leader for a specific event.

Type Parameters ​

Type ParameterDefault typeDescription
RanyThe type of the response.
E extends string | number | symbolkeyof EventsThe name of the event to request feedback for.

Parameters ​

ParameterTypeDescription
eventEThe name of the event to request feedback for.
...argsEvents[E]The arguments to pass to the event handler.

Returns ​

Promise<R>

A promise that resolves with the response from the event handler.


setAwaitResponseTimeout() ​

ts
setAwaitResponseTimeout(timeout: number): Swarm<Events>;

Set the timeout for waiting for a response from request() or collect().

Parameters ​

ParameterTypeDescription
timeoutnumberThe timeout in milliseconds to wait for a response.

Returns ​

Swarm<Events>

The current instance of the Swarm class for the current context.


instance() ​

ts
static instance<Events>(secret?: string | Secret<string>): Swarm<Events>;

Retreive the current instance of the Swarm class for the current context.

Type Parameters ​

Type ParameterDefault typeDescription
Events extends TypedEventMapTypedEventMapThe map of events and their arguments that can be emitted and listened to.

Parameters ​

ParameterType
secret?string | Secret<string>

Returns ​

Swarm<Events>

The current instance of the Swarm class for the current context.