Why do nodes only discover each other but do not connect when run from different computer on the same network

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?

Because you are listenning on loopback.
So for info all addresses on 127.*.*.* are bound to “loopback”.
That a virtual network card present on all computers that goes no where, it is used to create network connections from your computer to your computer.
Try replacing it by:

          '/ip4/0.0.0.0/tcp/0/ws',
          '/ip4/0.0.0.0/tcp/0',

0.0.0.0 means everything (note it might not work outside of your LAN if you don’t do port forward and announces).

1 Like

This indeed was the problem.

The fact that I still got the discovery log but not the connection log did not make it obvious to me that the issue was in the connection string. Do you by chance know why this was the case? How come the discovery mechanism were still able to discover each other even though I was listening on the loopback address?

Yes, that because discovery doesn’t involve connecting to you.
In your example it’s just receiving an mdns multicast packet.
If you were using webrtc-star it would have been fetching the info from the star server.
If you were using go-ipfs it would have been walking the DHT.

Thanks for the clarification.