Gossipsub out of the box peer discovery / connectivity?

I am new to LibP2P and have successfully got something up and running in NodeJS where I can start up nodes and they connect to a hardcoded node’s multi-address. I’ve also got the pubsub set up to use Gossipsub as in the examples.

When reviewing the spec (specs/gossipsub-v1.1.md at master · libp2p/specs · GitHub) and also the concepts (Publish/Subscribe :: libp2p Documentation) it looks like out-of-the-box peers should immediately start connecting to other peers, grafting, pruning, etc but in my environment they only connect to the hardcoded node.

Example:
Node1 starts up
Node2, 3, 4, 5 start up and is configured to connect to Node1

At this point if any node sends a message it goes to Node 1 sends the message to all the other nodes.

However when I look at the connectivity Node 2, 3, 4, 5 are only connected to Node 1 and not aware of the other nodes.

Based on the documentation I was hoping/expecting that as Nodes 2, 3, 4, 5, etc connected to Node 1 they would automatically discover the other nodes and start dialing/connecting directly but perhaps I am missing something easy.

Sorry if I haven’t explained myself well, thanks for the help and thanks for putting together this great set of code!

Hello @lukebelbina

Peer discovery is not a part of the pubsub subsystem. In the libp2p documentation link you can see that discovery modules need to be used, in order to discover peers that will result into established connections.

Unfortunately, js libp2p does not offer discovery out of the box to enable pubsub like go libp2p currently does. The meta issue to enable this in JS has a discussion on how this should work. In a short summary, nodes should use DHT (Delegate routers in JS for now, as the JS implementation of the DHT is not reliable yet) to announce to the network that they subscribe to a given topic and then try to find peers with the same interests over time.

While this is not implemented in JS, if you setup on your application delegate routers and manually do libp2p.contentRouting.provide(topicCid) and then libp2p.contentRouting.findProviders(topicCid), you will be able to discover multiaddrs of peers sharing the same pubsub interests and establish connections to them.

I hope this helps you

1 Like

OK perfect, really helpful. I’ll take that approach and get something working before seeing if there’s anything I can contribute back (whether source or documentation).

I will read the js gossip sub source code today to understand the internals more but to confirm, in the JS implementation once Peers are discovered and topics are known will the implementation manage balancing the connections over time?

You mean improving the connection mesh over time? If so, not by default. If you enable peer exchange and other peers also have peer exchange enable this would make libp2p discover other peers and eventually connect to them (if the maximum number of connection is not reached).