Demo some functionality with libp2p

Tram Ho

What to do now?

Let’s try to use some basic functions of libp2p to send ping messages between two peers with js.


  • Nodejs> 12.16.x

Setup folder

Configure libp2p

Next is to install libp2p

Basic setup

H, you have finished installing libp2p then h please configure a bit. We need 2 modules of libp2p: Transport and Crypto . However, according to their docs, it should also setup Stream Multiplexer . Start setting up each part.


Libp2p uses Transports to establish connections between peers through the network. You can configure as many Transports as you like, but in this demo you only need 1. There are many protocols supported but please select tcp.

After installation is complete, write a few lines of code so that libp2p can use TCP

In that Libp2p.create initialize and modules transport to choose the protocol and you can choose more protocols because the transport received is an array .

Connection Encryption

All connections must be encrypted to ensure user privacy. There are many Crypto modules developed for libp2p and in this demo use libp2p-secio

Add the secio section to:


Although Multiplexing is not required, it is advisable to learn it because it helps improve the efficiency of connections on the protocols that libp2p runs.

Add mplex part to:

Let’s try it

We have already configured the Transport , Crypto and Stream Multiplexer modules, so let’s run libp2p node. Use libp2p.start () and libp2p.stop () to start and stop the node.

A libp2p node needs to listen to an address on the protocol that we have installed. Then we will install more multiaddr to create a tcp multiaddress and add it to the node

Add multiaddr

Then test with the command node src/index and here’s the result:

Ping pong!

Everything has been well setup from protocol to security, … let’s try to connect them together.

We can use to send “ping” from one peer to another. Peer receiving “ping” will send back “pong” and so we know that peers can connect with each other. At the same time, we can check the delay between peers.

We need to pass peer multiaddress via the command line so we need more process

Then we try to transmit multiaddress from the commad line and try to ping how. And this will be the full code

Let’s start running before 1 node libp2p node src/index.js . Result :

Open another terminal for multiaddress address. For example, following the correct address above runs:

Result :

So these two peers were able to communicate with each other via multiplexed on a secure channel.

Connect with other peers

Once their libp2p node is up and running, it’s time to try connecting it to the public network. We can do this through peer discovery. Try this feature by creating a new file.

Peer Discovery

The general idea here is that there will be a fixed list of a few peers that will often be used for new nodes to join the network, but of course there will have to be some other discovery mechanism to ensure Tell me you can find other peers important to your app. There are several ways to find peer available online:

  • If you have the address of some other peer, the easiest way is to use libp2p-bootstrap
  • If your app is running on a browser, you can use libp2p-webrtc-star which will discover other peers based on peer sharing service.
  • Another approach is to use the basic libp2p-kad-dht , which will crawl the network and find new peers randomly based on the data on the peer it is connected to.

For simplicity, I will use libp2p-bootstrap and change the protocol to websocket (because the other nodes use this protocol). Install libp2p-bootstrap:

The code will look like this:

peer:discovery and peer:on are two events that libp2p provides you can see more here

Result :


Share the news now

Source : Viblo