From 48c132e3caa731cc7cca3f0258ecce6c2090dda4 Mon Sep 17 00:00:00 2001 From: Justin McAteer Date: Thu, 31 Aug 2023 14:00:20 -0500 Subject: [PATCH] Add options to broadcast our Peer info on seeing a new peer subscription. --- src/index.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index c49bba2..fe825f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,6 +29,17 @@ export interface PubsubPeerDiscoveryInit { * If true, we will not broadcast our peer data */ listenOnly?: boolean + + /** + * If true, we will broadcast our data when we see new peers on the peer discovery topic (default: false). + * listenOnly must not be set to false for this capability to be applied. + */ + broadcastOnSubscribe?: boolean + + /** + * Randomized backoff in milliseconds to wait before broadcasting on seeing a new subscription (default: 0.1 * interval). + */ + backoffOnSubscribe?: number } export interface PubSubPeerDiscoveryComponents { @@ -44,6 +55,8 @@ export class PubSubPeerDiscovery extends EventEmitter imple private readonly interval: number private readonly listenOnly: boolean private readonly topics: string[] + private readonly broadcastOnSubscribe: boolean + private readonly backoffOnSubscribe: number private intervalId?: ReturnType private readonly components: PubSubPeerDiscoveryComponents @@ -53,12 +66,16 @@ export class PubSubPeerDiscovery extends EventEmitter imple const { interval, topics, - listenOnly + listenOnly, + broadcastOnSubscribe, + backoffOnSubscribe, } = init this.components = components this.interval = interval ?? 10000 this.listenOnly = listenOnly ?? false + this.broadcastOnSubscribe = broadcastOnSubscribe ?? false + this.backoffOnSubscribe = backoffOnSubscribe ?? this.interval * 0.1; // Ensure we have topics if (Array.isArray(topics) && topics.length > 0) { @@ -112,6 +129,20 @@ export class PubSubPeerDiscovery extends EventEmitter imple return } + // Broadcast on Subscribe from other peers + if (this.broadcastOnSubscribe) { + pubsub.addEventListener('subscription-change', subChangeEvt => { + // Check if the PubSub peer cares about PubSub Peer Discovery + const discoverySubs = subChangeEvt.detail.subscriptions.filter(sub => this.topics.includes(sub.topic)); + + // The Peer is interested in PubSub Peer Discovery -> broadcast + if (discoverySubs.length > 0) { + const backoff = this.backoffOnSubscribe * Math.random() + setTimeout(() => { this._broadcast() }, backoff) + } + }) + } + // Broadcast immediately, and then run on interval this._broadcast()