Hey I am playing around with libp2p to implement in to an application I am development. I have had a few problems my current problem is that I have made client file and a master node for the bootstrap but currently the the client is discovering the bootstrap node but not conneting to the bootstrap.
Client node:
const createLibp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')
const Gossipsub = require('libp2p-gossipsub')
//const MulticastDNS = require('libp2p-mdns')
//const KadDHT = require('libp2p-kad-dht')
const Bootstrap = require('libp2p-bootstrap')
const { fromString } = require('uint8arrays/from-string')
const { toString } = require('uint8arrays/to-string')
const bootstrapers = [
'/ip4/0.0.0.0/tcp/2001/p2p/QmUbnhyxJizvXMyPqjL28b7ZaVH2CvgXiRzUhifJVn4HR6'
]
const createNode = async () => {
const node = await createLibp2p.create({
addresses: {
listen: ['/ip4/127.0.0.1/tcp/0']
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [NOISE],
pubsub: Gossipsub,
peerDiscovery: [Bootstrap]
},
config:{
peerDiscovery: {
[Bootstrap.tag]: {
interval: 1000,
enabled: true,
list: bootstrapers,
}
},
pubsub:{
emitSelf: true,
enabled: true
},
dht: {
enabled: true,
}
},
connectionManager: {
autoDial: true,
autoDialInterval: 1000
}
})
const topic = 'news'
await node.start()
console.log(node.peerId.toB58String())
node.connectionManager.on('peer:connect', (connection) => {
console.log('connection established to:', connection.remotePeer.toB58String())
})
node.on('peer:discovery', (peerID) => {
console.log('discovered:', peerID.toB58String())
})
await node.pubsub.subscribe(topic)
node.pubsub.on(topic, (msg) => {
console.log(`node recived: ${toString(msg.data)}`)
})
setTimeout( () => node.pubsub.publish(topic, fromString(`hello from node: ${node.peerId.toB58String()}`)), 9000)
}
createNode()
Bootstrap Node:
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')
const Gossipsub = require('libp2p-gossipsub')
// const MulticastDNS = require('libp2p-mdns')
// const KadDHT = require('libp2p-kad-dht')
const Bootstrap = require('libp2p-bootstrap')
const PeerId = require('peer-id')
const { fromString } = require('uint8arrays/from-string')
const { toString } = require('uint8arrays/to-string')
const bootstrapers = [
'/ip4/120.23.160.162/tcp/2001/p2p/QmUbnhyxJizvXMyPqjL28b7ZaVH2CvgXiRzUhifJVn4HR6'
]
const createNode = async () => {
const id = await PeerId.createFromJSON({
id: 'QmUbnhyxJizvXMyPqjL28b7ZaVH2CvgXiRzUhifJVn4HR6',
privKey: 'CAASpwkwggSjAgEAAoIBAQDQI3/PJaLmrDvrhEmCj6RZpFdm76eV8legnmIDJzT7/4Gk6y+k+NOU1lc0KQ+H3v0seP/5PjgNO8OZLeiQOxZ8EKilRhZ3+l61YmIWpvdaXpiS68WCl4czH3ndlMaG0o1GX/D/OhnuevyXOgLZKrY0x/mllC9UFmpxGJl88We9KronXuLF6cDQX6FRYl1nZvEdoFqYwbrg3NJSAuu2SO3ndFsOKTkcIbbsIW/XV76YMOYjHQLTYQqGevqdJolUxs44aU4aB0yX1Iegj3VSz4ShfJVys4//r4B/8+F4jEpK7qSdMkkHxD0bjXCMSB5CLBER2MVawPVrgdY0reNk5YPtAgMBAAECggEAXkhmYfhjhWhpsDL5dZoJ0c1/BhLzcBoaFZkJPvJoruo72O/sYieQ+G/riqY68cpSFu4szcOgcmYrIV1JZuRNOB/3tP49uVWBx1psomQ50gbdyFfi3Vg0lzTf6D63e2UvN7zIF2VDT+MIPpy5o5QUe25GqPmqQsXwq5CAHrffzTEXTwr0fFx3pFR5Qhj4GXSGUJjlcaaUENvL8P9uLLKvtBqra35J4jVGFHYA5gs3SfG1OAZzfX2ejnyNLSIyllBsk6gRGr84dBCnjts6jAECnBx/Y3ZvP68mrhveFb4pfjAmxk8qH84f6XMUWZ8GVpv9N+bg9SBIjsGx7Xv/NORQxQKBgQDxqpDjyT89kUs/dN79F+kwkAe5+t9LfELiPklhVqaXPYO06VuFi/cDG5Y56HQbkjJAQs6hmkj+KcSbsajdMZcZPr4MT8bdBNx3SIbGluosXn1C9WGHvNMz6i4OsvZnOb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JVys4//r4B/8+F4jEpK7qSdMkkHxD0bjXCMSB5CLBER2MVawPVrgdY0reNk5YPtAgMBAAE='
});
const node = await createLibp2p.create({
peerId: id,
addresses: {
listen: ['/ip4/127.0.0.1/tcp/2001']
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [NOISE],
peerDiscovery: [Bootstrap],
pubsub: Gossipsub
},
config:{
peerDiscovery: {
[Bootstrap.tag]: {
interval: 1000,
enabled: true,
list: bootstrapers
}
},
pubsub:{
emitSelf: true,
enabled: true
},
dht: {
enabled: true
},
},
connectionManager: {
autoDial: true,
autoDialInterval: 1000
}
});
const topic = 'news'
await node.start()
console.log(node.peerId.toB58String())
node.connectionManager.on('peer:connect', (connection) => console.log('connection established to:', connection.remotePeer.toB58String()))
node.on('peer:discovery', (peerID) => console.log('discovered:', peerID.toB58String()))
await node.pubsub.subscribe(topic)
node.pubsub.on(topic, (msg) => {
console.log(`node recived: ${toString(msg.data)}`)
})
setTimeout( () => node.pubsub.publish(topic, fromString(`hello from node: ${node.peerId.toB58String()}`)), 9000)
}
createNode()
this is the terminal for client:
QmSam1B1vSexJbupz5Zc7mU3HZMCTFiijckJLCF4HSKJ5K
discovered: QmUbnhyxJizvXMyPqjL28b7ZaVH2CvgXiRzUhifJVn4HR6
node recived: hello from node: QmSam1B1vSexJbupz5Zc7mU3HZMCTFiijckJLCF4HSKJ5K
Client will never display connect but will if i use MulticastDNS can you tell me what I am doing wrong.
Thanks for your help in advance.