Hey new to using libp2p and this is my second attempt in the last year to get it to work ( gave up the first time haha).
So I have made two separate nodes. My first node (boot-node) witch has a persistent id for easy discovery, my second node (client) is just a standalone separate node that connects to my boot-node.
As of now both nodes run smooth and the connection manger event listener for peer:connect on both nodes trigger my console.log, so i presume both nodes are connected. (correct me if I’m wrong please)
After both nodes “connect” my pub sub function is implemented on the client side and attempts to send the nodes multiaddrs of the client to the boot-node. This is when nothing happens the clients emits to self and i see the publish there but the boot-node never receives the publish.
Client.js
import { createLibp2p } from 'libp2p'
import ipily from 'ipily'
import { Bootstrap } from '@libp2p/bootstrap'
import { TCP } from '@libp2p/tcp'
import { Noise } from '@chainsafe/libp2p-noise'
import { Mplex } from '@libp2p/mplex'
import { FloodSub } from '@libp2p/floodsub'
import { createFromJSON } from '@libp2p/peer-id-factory'
import { toString } from 'uint8arrays/to-string'
import { fromString } from 'uint8arrays/from-string'
const main = async () => {
const bootstrapers = ['/ip6/::/tcp/4002/p2p/QmYMxFJAWQfwM3nWCChySdJSn4X9T4tkkTuUgYKaQXytg8']
const ip = await ipily()
const node = await createLibp2p({
addresses: {
// add a listen address (localhost) to accept TCP connections on a random port
listen: [`/ip6/${ip}/tcp/4001`]
},
transports: [new TCP()],
connectionEncryption: [new Noise()],
streamMuxers: [new Mplex()],
peerDiscovery: [new Bootstrap({
interval: 60e3,
list: bootstrapers
})],
pubsub: new FloodSub({
emitSelf: true,
enabled: true
}),
connectionManager: {
autoDial: true
}
})
const topic = 'peer-bradcast'
// start libp2p
await node.start()
console.log('libp2p has started')
// print out listening addresses
node.getMultiaddrs().forEach((addr) => {
console.log('listening on addresses:', addr.toString())
})
node.pubsub.subscribe(topic)
node.connectionManager.addEventListener('peer:connect', (evt) => {
const connection = evt.detail
console.log(`connection to : ${connection.remotePeer.toString()}`)
setInterval(() => {node.getMultiaddrs().forEach((addr) => {
node.pubsub.publish(topic, fromString(addr.toString()))
})}, 5000)
})
node.pubsub.addEventListener('message', (evt) => {
console.log('received:', toString(evt.detail.data))
})
}
main().then().catch(console.error)
Boot-node.js
import { createLibp2p } from 'libp2p'
import { Bootstrap } from '@libp2p/bootstrap'
import { TCP } from '@libp2p/tcp'
import { Noise } from '@chainsafe/libp2p-noise'
import { Mplex } from '@libp2p/mplex'
import { createFromJSON } from '@libp2p/peer-id-factory'
import { FloodSub } from '@libp2p/floodsub'
import { Multiaddr } from '@multiformats/multiaddr'
import { toString } from 'uint8arrays/to-string'
import { fromString } from 'uint8arrays/from-string'
import ipily from 'ipily'
const main = async () => {
const myPeerId = await createFromJSON({
id: 'QmYMxFJAWQfwM3nWCChySdJSn4X9T4tkkTuUgYKaQXytg8',
privKey: 'CAAS4gQwggJeAgEAAoGBAK3ZWsNyweiltshFXVqViO/+vSKnKHA8v/RMIpJK7TPlQlDEjkYQO+AOCj+NJ35cQZLet3RE6MOf4nSD5deKX15v+nS5xcCaDR3uHnq/RK5zm8CveEodGBuPplbcS9i+0z8FIHdjZRBQ0UYn6BZb0AIe06UPoL6slhyn8kae6+9rAgMBAAECgYEAn/GT/rNq0Wb8xj6DB98BsIO2cNZHbxOFKvaM6/kBSLiJDZP4rV8sJxPju9pa1nd8YOqE7/SiDbDggIAFCwukdtHzpMQTKoEVFXSvkxV4J18VdkpEHpy19F2qWrbVExiTmZo3JBgoVDFvT79vDWlMLKYu0559Fn/x1lCALVEIdJECQQDim9P8zdtRdvqm6ykHHsqe1nzFTd5hbslQiSwggoQoH1eLxKVl+LZWSZ3Zv30RvSvOtuFFtQvcNSc1vgRUkCMDAkEAxGW6YTM5T5ouh8u6JPZfKoccDEH22fPl9ZC11Ve/T/gahIsNquIdkg00K9c4aWErAagXXrynuU0rBKBs7hgheQJBAOI3zZhBqP/aVr7rYznIc129oTEWWznI7w+G4JZTtLqZDzxoQvVZJC1fsET/9EsRHfpudtSvt/dM2Ke92XTRryECQCfV7nj6t9wyjM+UfhwzMNhicUj3NEe/Fdy6TneIhdCFpXNO9SW9GXxfWz6tN88AG4YJfzwDDg4i3d/hXlbo5BkCQQDP3XAQOuOxIjQFQhtkbzmePJeUWCrDF/9/wCDjhZI/Qslap1Twc4jgAFlPSQN4PlPrEYAXBGl8ErWaYVv8KgX8',
pubKey: 'CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAK3ZWsNyweiltshFXVqViO/+vSKnKHA8v/RMIpJK7TPlQlDEjkYQO+AOCj+NJ35cQZLet3RE6MOf4nSD5deKX15v+nS5xcCaDR3uHnq/RK5zm8CveEodGBuPplbcS9i+0z8FIHdjZRBQ0UYn6BZb0AIe06UPoL6slhyn8kae6+9rAgMBAAE='
});
const ip = await ipily()
const node = await createLibp2p({
addresses: {
// add a listen address (localhost) to accept TCP connections on a random port
listen: [`/ip6/${ip}/tcp/4002`]
},
peerId: myPeerId,
transports: [new TCP()],
connectionEncryption: [new Noise()],
streamMuxers: [new Mplex()],
pubsub: new FloodSub({
emitSelf: true,
enabled: true
}),
connectionManager: {
autoDial: true
}
})
const topic = 'peer-bradcast'
// start libp2p
await node.start()
console.log('libp2p has started')
// print out listening addresses
node.getMultiaddrs().forEach((addr) => {
console.log('listening on addresses:', addr.toString())
})
node.pubsub.subscribe(topic)
node.connectionManager.addEventListener('peer:connect', (evt) => {
const connection = evt.detail
console.log(`connection to : ${connection.remotePeer.toString()}`)
})
node.pubsub.addEventListener('message', (evt) => {
console.log('received:', toString(evt.detail.data))
})
}
main().then().catch(console.error)
Pleas let me know what I’m doing wrong thanks in advance.