This implements peer discovery using gossip. A peer first connects to peers
known to it vai its peer book and asks for more peers. This processes continues
untill targetNumberOfPeers
is reached.
npm install libp2p-gossip-discovery
const GossipDiscovery = require('gossip-discovery')
const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const multiplex = require('libp2p-multiplex')
class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
const discovery = new GossipDiscovery(this, 10)
const modules = {
transport: [
new TCP()
],
connection: {
muxer: [
multiplex
]
}, discovery: [discovery]
}
super(modules, peerInfo, peerBook, options)
discovery.attach(this)
}
}
////
const node = new Node(peerInfo)
node.start(() => {
console.log('started!')
})
Parameters
targetNumberOfPeers
Number the max number of peers to add to the peer book
Attach an instance of libp2p to the discovery instance
Parameters
node
Object the libp2p instance
starts the gossip process, this is called by libp2p but if you are using this standalone then this needs to be called
Parameters
cb
Function a callback
stop discovery, this is called by libp2p but if you are using this standalone then this needs to be called
All code and designs are open sourced under GPL V3.