I am trying to get up to speed with js-libp2p and I am poking around with this code snippet:
const Libp2p = require("libp2p")
const WebSockets = require('libp2p-websockets')
const Tcp = require('libp2p-tcp')
const { NOISE } = require('libp2p-noise')
const MPLEX = require('libp2p-mplex')
const Mdns = require('libp2p-mdns')
async function main() {
const node = await Libp2p.create({
modules: {
transport: [WebSockets, Tcp],
connEncryption: [NOISE],
streamMuxer: [MPLEX],
peerDiscovery: [Mdns]
},
addresses: {
listen: [
'/ip4/127.0.0.1/tcp/0/ws',
'/ip4/127.0.0.1/tcp/0',
]
}
})
await node.start()
node.on('peer:discovery', (peer) => {
console.log(`Discovered ${peer}`)
})
node.connectionManager.on('peer:connect', (connection) => {
console.log(`Connection established ${connection.remotePeer.toB58String()}`)
})
}
main()
If I run it from two consoles on the same computer, on starting the second console I get the following expected output:
Discovered bafzbeia7bnohivu4lnyvzd5f4vix2xsru4sidmgitcp5u2ysepxwct5rwu
Connection established QmQRqPvVKDYUKrNYc8SsFwyK2kkxq3zUSojvmDUqNzoy32
Connection established QmQRqPvVKDYUKrNYc8SsFwyK2kkxq3zUSojvmDUqNzoy32
Connection established QmQRqPvVKDYUKrNYc8SsFwyK2kkxq3zUSojvmDUqNzoy32
and from the other console also:
Discovered bafzbeieeqfz3whgaj7hbxbczkxs3vue2yxid4cwpdqkrtzcrpkmepuqzj4
Connection established QmXFu1kP9wWu42vtREEdXhdWc4uLHZ4t5r3fYtfbmb66y4
Connection established QmXFu1kP9wWu42vtREEdXhdWc4uLHZ4t5r3fYtfbmb66y4
Connection established QmXFu1kP9wWu42vtREEdXhdWc4uLHZ4t5r3fYtfbmb66y4
But if I move the same script and run it on another computer on the same network, I only see the Discovered log. The connection log never gets printed out. For example I only see this:
âžś my_node node node.js
Discovered bafzbeienf65d3slqvblq3sqoedltuqorakqfkdnyodocul52gptdrqhqx4
Does anybody have an explanation why this is the case? And also what do I need to do to have full connection? Because as I poke around with libp2p I would prefer to have the nodes run on two computers.
Also maybe explain the difference between peer:discovery
event and peer:connect
event? I mean how is peer discovery different from peer connected?
PS: Also I noticed that the connection log keeps getting printed at interval. Why is that also the case?