js-libp2p-gossipsub icon indicating copy to clipboard operation
js-libp2p-gossipsub copied to clipboard

Message delivery not working if publisher is sending message to early after connect

Open koh-osug opened this issue 1 year ago • 0 comments

The subscribe function is called from a subscriber with node.services.pubsub.subscribe(PEER_SUBSCRIPTION_TOPIC) and the other publishing peer is sending a notification with node.services.pubsub.publish(PEER_SUBSCRIPTION_TOPIC, new Uint8Array(BSON.serialize({ipAddress: argv.ipAddress}).buffer)). What I notice is that the publishing only works if the subscription of the other peer is know, i.e. if the publish is executed too early before the subscribe is known, publish is not doing anything and any listener node.services.pubsub.addEventListener('message', (message) => {...} is never called in the subscriber. To make it work I have to wait until the publishing peer is connected and I have to use a delay in the publisher like:

        setTimeout( () => {
            node.services.pubsub.publish(PEER_SUBSCRIPTION_TOPIC, new Uint8Array(BSON.serialize({ipAddress: argv.ipAddress}).buffer))
        }, 5000)

What is the correct way to send a message from a publisher, so that a connecting client can receive it without using this delay? is there a callback?

koh-osug avatar Jan 09 '24 01:01 koh-osug