Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gossip pubsub error, PublishError.InsufficientPeers #233

Closed
chiro-hiro opened this issue Aug 16, 2023 · 3 comments
Closed

Gossip pubsub error, PublishError.InsufficientPeers #233

chiro-hiro opened this issue Aug 16, 2023 · 3 comments

Comments

@chiro-hiro
Copy link

I'm using the latest version of helia@1.3.12, I have this example code it wasn't work for me even there are several peers are connected. Am I do anything wrong?.

import { createHelia } from "helia";

(async () => {
  const helia = await createHelia();

  helia.libp2p.services.pubsub.subscribe("news");

  helia.libp2p.services.pubsub.addEventListener("message", async (evt) => {
    console.log(
      `node1 received: ${new TextDecoder().decode(evt.detail.data)} on topic ${
        evt.detail.topic
      }`
    );
  });

  setInterval(async () => {
    console.log(`Number of peers: ${helia.libp2p.getPeers().length}`);

    helia.libp2p.services.pubsub.publish(
      "news",
      new TextEncoder().encode("Chiro here")
    );
  }, 5000);

})();
@achingbrain
Copy link
Member

This error happens when there are no peers listening on the "news" topic - it means the message you publish will not be received by anyone.

You can check the peers subscribed to your topic with helia.libp2p.services.pubsub.getSubscribers('news').

@chiro-hiro
Copy link
Author

Hi @achingbrain, thank for your answer.

I've just tried the demo from the @chainsafe/libp2p-gossipsub. I followed your suggestion and connect two nodes directly, it's start working as expected.

Then I'm start to boot up two nodes without direct connect. It took me 15 minutes but these two nodes can't discover each other.

Here is the multi addresses of my 1st node.

[
  Multiaddr(/ip4/127.0.0.1/tcp/65435/p2p/12D3KooWQY8vANjwpYGYssh16GgKT7aT666f7y3N6c1nRy2ku9qw),
  Multiaddr(/ip4/192.168.1.4/tcp/65435/p2p/12D3KooWQY8vANjwpYGYssh16GgKT7aT666f7y3N6c1nRy2ku9qw)
]

@SgtPooki
Copy link
Member

@chiro-hiro you may want to check out the recent example I put together for lan discovery: ipfs-examples/helia-examples#136

You will likely want something like this:

helia.libp2p.services.pubsub.addEventListener('subscription-change', (evt) => {
  if (helia.libp2p.services.pubsub.getSubscribers(pubSubTopic).length !== 0) {
    // we're subscribed, and so is another node, so we can publish 
    helia.libp2p.services.pubsub?.publish(pubSubTopic, new TextEncoder().encode('wut-CID'))
  }
})

@SgtPooki SgtPooki closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

3 participants