Hwo to use libp2p in browser


I have a libp2p node in a backend server. I want to start a libp2p also in the browser that communicates with the one on the backend server.

If I run:

const ma = multiaddr('/ip4/')
  //const latency = await node2.ping(ma)

I get error:

The dial request has no valid addresses

Also, thaks to the peer discovery I see that the browser libp2p node is connected to many peers I’d like to have it connected only to my backend libp2p node. How can that be done?


Browsers can’t connect to ip4 nodes, for your use case you would probably want to use a WebSocket. There is also a direct browser to browser tool WebRTC, those are the only two modes available.