I want to welcome you to ! This guide will walk you through setting up a fully functional libp2p node, so that you can take control of the spacecraft 🚀 from that point on and build your application. libp2p Background P2P based systems and applications have been around for several decades now. Such projects have well known challenges and difficulties that need to be addressed regarding their networking layer. Over the years, new projects have implemented p2p networking primitives again and again. These projects usually had their networking layer tight with the application layer and were not open source. Moreover, some projects did not even reach the application layer development as a consequence of all the networking challenges that needed to be solved before getting into it. All things considered, during these decades the available projects did not contribute to a rapid growth of the p2p ecosystem. One of the primary goals of libp2p is to fill this gap by enabling application and system developers to focus on their use cases and business logic while relying on a composable networking stack. What is libp2p? Libp2p is a composable and modular networking stack, which was designed to be able to become the networking layer of any type of p2p system. Its modular nature is the key to enable such a variety of projects, while also being upgradable for future network requirements. Understanding where libp2p fits is important to understand how it is designed and how it can be used. Different p2p systems have different needs and requirements, while they might run in different environments and have different hardware capabilities. Considerations before starting Before you start with libp2p, it is important to understand what environments you expect your application to run on. Nowadays, you can run JavaScript code in several different environments, some of them with their own particularities. While the libp2p core codebase aims to work in multiple environments, there are some limitations that are not possible to overcome at the time of writing. As an example, browsers do not support listening for connections via WebSockets, but only to dial known addresses, as they are designed for the traditional client-server model. As a consequence of these limitations, some environments will require to think about a network topology and additional infrastructure as a way to achieve connectivity, as well as to provide a smoother experience to the end user. There is a wide variety of libp2p modules available to plug in, as well as different ways to configure each one. Libp2p empowers the application layer to select the modules they need and nothing else. This flexibility aims to fulfil every network requirement, environment constraints and hardware capabilities. Create your libp2p node with js-libp2p It is usually recommended to start with a base configuration of libp2p and build upon it, specially for new users that are not entirely familiar with every single piece of the libp2p stack. The most important documents to have handy are the and the . The former is divided into two main parts, one for creating a libp2p node and one to interact with it, while the latter is focused on configuring each type of libp2p module. The configuration of libp2p is a key part of its structure. Each project should go through what libp2p can offer and select what is important for them. Regardless of how it is configured, its top level API will always be the same. API documentation Configuration documentation The first step is to install libp2p in your project with your favourite JavaScript Package Manager. In this guide, we will configure the libp2p node in several stages, as this can make troubleshooting configuration issues much easier. It is important to specify that the following content is based on as the way libp2p is configured might change in upcoming releases. libp2p@0.29 Basic setup With libp2p installed, let’s configure the absolute minimum needed to get your node running. The only modules libp2p requires are a and a module. Moreover, it is highly recommended that a basic setup should also have a configured. The create options can be checked in the . Transport Crypto Stream Multiplexer API.md#create Transport Libp2p uses Transports to establish connections between peers over the network. You can configure one Transport, or as many as you like. Supporting more Transports will improve the ability for other nodes on the network to communicate with you. You should select Transports according to the runtime where your application will run. You can see a of some of the available Transports. As an example, in a browser environment and are typically installed. list libp2p-websockets libp2p-webrtc-star If you need a transport that is not available in the ecosystem, you can easily create your own. The only requirement of a libp2p transport is to be compliant with the . transport interface Crypto In a p2p context, encryption is an essential component for the communication on the libp2p network. Every connection is required to be encrypted to ensure security for everyone. is the to use at the moment. is being but other encryption modules might appear soon. libp2p-noise recommended module libp2p-secio deprecated If you want to use your own connection encryption module, you just need to guarantee that it is compliant with the . crypto interface Multiplexers Multiplexers are not required by libp2p, but they are super important for the efficiency of the connections using several protocols that libp2p will run. It allows libp2p to run several of its internal protocols, like Identify, as well as allow your application to easily run any number of protocols over a single connection. Running libp2p With a Transport, Crypto and Stream Multiplexer modules specified, you can your libp2p node. However, you also need to specify its listening addresses, so that your transports can listen for connections from other peers. Otherwise, your node will be a peer. start “dial only” This will work perfectly in a Node.js environment, but it will not start in a browser context. The listen multiaddr aims to listen for new websocket connections on the local port. As previously stated, browsers do not support listening for connections. As a result, this type of multiaddr should not be used if your application will run in a browser environment. However, you can still use the websockets transport to dial other peers that can listen for websocket connections. /ip4/127.0.0.1/tcp/8000/ws 8000 The described issue is one of the main reasons to have transport. The other multiaddr aims to listen on a different machine that can listen for new connections. The star server on will listen for new connections and relay them to the appropriate peer. More than listening from connection on behalf of other peers, the star server acts as a peer discovery mechanism and informs the connected peers once a new peer joins. Bear in mind that this is a public available star server and its availability is not guaranteed. You should your own for production.You can check the complete libp2p-webrtc-star /dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star wrtc-star1.par.dwebops.pub setup API documentation You can check the complete to understand what you can do with your node from now on. API documentation Customize your js-libp2p node Now that you can have connectivity with other nodes in the network, it is time to select the modules that you need for your application, as well as to tune the node according to your needs. There are several other modules that can be added, in order to fulfil the needs of a p2p application. These include Peer Discovery, Pubsub, Content Routing and Peer Routing, among others. Bear in mind that you should only add the modules you need. Peer Discovery One of the most important characteristics of a typical p2p application is its ability to find other peers on the network. Libp2p provides discovery protocols. Some of them are also dependent of the runtime of the node, like , which is not supported in a web browser. several libp2p-mdns You can also specify your configuration for each peer discovery module you provide. It is also important to point out that transports with built in peer discovery capabilities (e.g ) are automatically added as discovery modules (but can be disabled). libp2p-webrtc-star With Peer Discovery modules added, libp2p will handle everything and add the discovered peers to its Peer Store. Pubsub Some applications will potentially need a pubsub message service for real-time communication. There are available, but is the recommended router to use thanks to its efficiency and security measures. Once you have a pubsub router in your node, you are able to use the , where the most common methods are and . two pubsub routers libp2p-gossipsub Pubsub API Methods subscribe publish Content and Peer Routing offers a way to find where content lives on the network, while provides a mechanism to find other peers on the network by making queries. There are currently two implementations for each of these, the (Distributed Hash Table) which provides both content and peer routing, and one implementation for content routing and one for peer routing. The delegate routers aim to leverage more capable nodes to issue DHT queries on behalf of the requesting peer. It is common to leverage nodes for this purpose. Content routing Peer Routing DHT delegate router go-ipfs You can have multiple routers, as well as the DHT at the same time. If that is the case, libp2p will start by trying the DHT. If it is not successful, it will try each router until it succeeds. It is important pointing out that peers with less capabilities are recommended to use the DHT. not Create your own protocol So far, we went through the basics for building a regular p2p app. We may decide which modules to use according to our needs, or even create new ones. But, libp2p also allows the creation of new protocols. Libp2p is a , where custom protocols operate in a similar fashion as a RESTful API. Each protocol has an identifier and a handler. As an example, uses the protocol on top of libp2p for custom data exchange between peers. “modular system of protocols” IPFS ipfs-bitswap After you have your custom protocol, you just need to hook it into libp2p. The will listen for stream messages under the protocol and call the provided handler. handle What is next? Once you have your networking building blocks ready to be used by your app, you can start focusing on it! 🎉 But before going into production, you should setup your own infrastructure. The code examples here use public infrastructure provided by Protocol Labs for experimenting, but they are not meant to be used in production and their availability is not guaranteed. According to your requirements and used modules, you might need to setup webrtc-star servers, delegate routers, or even circuit relay nodes. There are a few new features expected for the upcoming libp2p releases that will help with some of the pain points covered here regarding connectivity and discoverability. Stay tuned and let’s distribute the web together! ❤️ At MOXY we’ve been contributing to libp2p and also leveraging it to deliver distributed applications, namely and , among a few others underway. If you are looking for help with a project that could benefit from p2p capabilities, . MOXY has been working on several projects on top of libp2p, namely Nomios Discussify tell us a bit more Nomios If you have any question, issue, or even new ideas with , you are welcome to reach out to me directly on , create a topic in the libp2p or open Github issues in any . libp2p Twitter discussion forum libp2p repo Written by Vasco Santos