Skip to content

Commit 17b6a3f

Browse files
docs: config (#495)
* docs: new api * chore: new iteration * chore: apply suggestions from code review Co-Authored-By: Alan Shaw <[email protected]> * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <[email protected]> * chore: address review * docs: add events * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <[email protected]> * docs: configuration * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <[email protected]> * chore: update peer routing description Co-Authored-By: Yusef Napora <[email protected]> * chore: decouple examples * chore: address pr comment * fix: connection encryption is required * chore: apply review suggestion Co-Authored-By: Jacob Heun <[email protected]>
1 parent 4f75868 commit 17b6a3f

File tree

1 file changed

+384
-0
lines changed

1 file changed

+384
-0
lines changed

doc/CONFIGURATION.md

Lines changed: 384 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,384 @@
1+
# Configuration
2+
3+
* [Overview](#overview)
4+
* [Modules](#modules)
5+
* [Transport](#transport)
6+
* [Stream Multiplexing](#stream-multiplexing)
7+
* [Connection Encryption](#connection-encryption)
8+
* [Peer Discovery](#peer-discovery)
9+
* [Content Routing](#content-routing)
10+
* [Peer Routing](#peer-routing)
11+
* [DHT](#dht)
12+
* [Pubsub](#pubsub)
13+
* [Customizing Libp2p](#customizing-libp2p)
14+
* [Examples](#examples)
15+
* [Configuration examples](#configuration-examples)
16+
17+
## Overview
18+
19+
libp2p is a modular networking stack. It's designed to be able to suit a variety of project needs. The configuration of libp2p is a key part of its structure. It enables you to bring exactly what you need, and only what you need. This document is a guide on how to configure libp2p for your particular project. Check out the [Configuration examples](#configuration-examples) section if you're just looking to leverage an existing configuration.
20+
21+
Regardless of how you configure libp2p, the top level [API](./API.md) will always remain the same. **Note**: if some modules are not configured, like Content Routing, using those methods will throw errors.
22+
23+
## Modules
24+
25+
`js-libp2p` acts as the composer for this modular p2p networking stack using libp2p compatible modules as its subsystems. For getting an instance of `js-libp2p` compliant with all types of networking requirements, it is possible to specify the following subsystems:
26+
27+
- Transports
28+
- Multiplexers
29+
- Connection encryption mechanisms
30+
- Peer discovery protocols
31+
- Content routing protocols
32+
- Peer routing protocols
33+
- DHT implementation
34+
- Pubsub router
35+
36+
The libp2p ecosystem contains at least one module for each of these subsystems. The user should install and import the modules that are relevant for their requirements. Moreover, thanks to the existing interfaces it is easy to create a libp2p compatible module and use it.
37+
38+
After selecting the modules to use, it is also possible to configure each one according to your needs.
39+
40+
Bear in mind that only a **transport** and **connection encryption** are required, while all the other subsystems are optional.
41+
42+
### Transport
43+
44+
> In a p2p system, we need to interact with other peers in the network. Transports are used to establish connections between peers. The libp2p transports to use should be decided according to the environment where your node will live, as well as other requirements that you might have.
45+
46+
Some available transports are:
47+
48+
- [libp2p/js-libp2p-tcp](https://github.com/libp2p/js-libp2p-tcp)
49+
- [libp2p/js-libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star)
50+
- [libp2p/js-libp2p-webrtc-direct](https://github.com/libp2p/js-libp2p-webrtc-direct)
51+
- [libp2p/js-libp2p-websockets](https://github.com/libp2p/js-libp2p-websockets)
52+
- [libp2p/js-libp2p-utp](https://github.com/libp2p/js-libp2p-utp) (Work in Progress)
53+
54+
You should take into consideration that `js-libp2p-tcp` and `js-libp2p-utp` are not available in a **browser** environment.
55+
56+
If none of the available transports fulfills your needs, you can create a libp2p compatible transport. A libp2p transport just needs to be compliant with the [Transport Interface](https://github.com/libp2p/js-interfaces/tree/master/src/transport).
57+
58+
If you want to know more about libp2p transports, you should read the following content:
59+
60+
- https://docs.libp2p.io/concepts/transport
61+
- https://github.com/libp2p/specs/tree/master/connections
62+
63+
### Stream Multiplexing
64+
65+
> Libp2p peers will need to communicate with each other through several protocols during their life. Stream multiplexing allows multiple independent logical streams to share a common underlying transport medium, instead of creating a new connection with the same peer per needed protocol.
66+
67+
Some available stream multiplexers are:
68+
69+
- [libp2p/js-libp2p-mplex](https://github.com/libp2p/js-libp2p-mplex)
70+
71+
If none of the available stream multiplexers fulfills your needs, you can create a libp2p compatible stream multiplexer. A libp2p multiplexer just needs to be compliant with the [Stream Muxer Interface](https://github.com/libp2p/js-interfaces/tree/master/src/stream-muxer).
72+
73+
If you want to know more about libp2p stream multiplexing, you should read the following content:
74+
75+
- https://docs.libp2p.io/concepts/stream-multiplexing
76+
- https://github.com/libp2p/specs/tree/master/connections
77+
- https://github.com/libp2p/specs/tree/master/mplex
78+
79+
### Connection Encryption
80+
81+
> A connection encryption mechanism must be used, in order to ensure all exchanged data between two peers is encrypted.
82+
83+
Some available connection encryption protocols:
84+
85+
- [libp2p/js-libp2p-secio](https://github.com/libp2p/js-libp2p-secio)
86+
87+
If none of the available connection encryption mechanisms fulfills your needs, you can create a libp2p compatible one. A libp2p connection encryption protocol just needs to be compliant with the [Crypto Interface](https://github.com/libp2p/js-interfaces/tree/master/src/crypto).
88+
89+
If you want to know more about libp2p connection encryption, you should read the following content:
90+
91+
- https://docs.libp2p.io/concepts/secure-comms
92+
- https://github.com/libp2p/specs/tree/master/connections
93+
94+
### Peer Discovery
95+
96+
> In a p2p network, peer discovery is critical to a functioning system.
97+
98+
Some available peer discovery modules are:
99+
100+
- [js-libp2p-mdns](https://github.com/libp2p/js-libp2p-mdns)
101+
- [js-libp2p-bootstrap](https://github.com/libp2p/js-libp2p-bootstrap)
102+
- [js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
103+
- [js-libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star)
104+
105+
If none of the available peer discovery protocols fulfills your needs, you can create a libp2p compatible one. A libp2p peer discovery protocol just needs to be compliant with the [Peer Discovery Interface](https://github.com/libp2p/js-interfaces/tree/master/src/peer-discovery).
106+
107+
If you want to know more about libp2p peer discovery, you should read the following content:
108+
109+
- https://github.com/libp2p/specs/blob/master/discovery/mdns.md
110+
111+
### Content Routing
112+
113+
> Content routing provides a way to find where content lives in the network. It works in two steps: 1) Peers provide (announce) to the network that they are holders of specific content and 2) Peers issue queries to find where that content lives. A Content Routing mechanism could be as complex as a DHT or as simple as a registry somewhere in the network.
114+
115+
Some available content routing modules are:
116+
117+
- [js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
118+
- [js-libp2p-delegated-peer-routing](https://github.com/libp2p/js-libp2p-delegated-peer-routing)
119+
120+
If none of the available content routing protocols fulfills your needs, you can create a libp2p compatible one. A libp2p content routing protocol just needs to be compliant with the [Content Routing Interface](https://github.com/libp2p/js-interfaces/tree/master/src/content-routing). **(WIP: This module is not yet implemented)**
121+
122+
If you want to know more about libp2p content routing, you should read the following content:
123+
124+
- https://docs.libp2p.io/concepts/content-routing
125+
126+
### Peer Routing
127+
128+
> Peer Routing offers a way to find other peers in the network by issuing queries using a Peer Routing algorithm, which may be iterative or recursive. If the algorithm is unable to find the target peer, it will return the peers that are "closest" to the target peer, using a distance metric defined by the algorithm.
129+
130+
Some available peer routing modules are:
131+
132+
- [js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
133+
- [js-libp2p-delegated-peer-routing](https://github.com/libp2p/js-libp2p-delegated-peer-routing)
134+
135+
If none of the available peer routing protocols fulfills your needs, you can create a libp2p compatible one. A libp2p peer routing protocol just needs to be compliant with the [Peer Routing Interface](https://github.com/libp2p/js-interfaces/tree/master/src/peer-routing). **(WIP: This module is not yet implemented)**
136+
137+
If you want to know more about libp2p peer routing, you should read the following content:
138+
139+
- https://docs.libp2p.io/concepts/peer-routing
140+
141+
### DHT
142+
143+
> A DHT can provide content and peer routing capabilities in a p2p system, as well as peer discovery capabilities.
144+
145+
The DHT implementation currently available is [libp2p/js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht). This implementation is largely based on the Kademlia whitepaper, augmented with notions from S/Kademlia, Coral and mainlineDHT.
146+
147+
If this DHT implementation does not fulfill your needs and you want to create or use your own implementation, please get in touch with us through a github issue. We plan to work on improving the ability to bring your own DHT in a future release.
148+
149+
If you want to know more about libp2p DHT, you should read the following content:
150+
151+
- https://docs.libp2p.io/concepts/protocols/#kad-dht
152+
- https://github.com/libp2p/specs/pull/108
153+
154+
#### Pubsub
155+
156+
> Publish/Subscribe is a system where peers congregate around topics they are interested in. Peers interested in a topic are said to be subscribed to that topic and should receive the data published on it from other peers.
157+
158+
Some available pubsub routers are:
159+
160+
- [libp2p/js-libp2p-floodsub](https://github.com/libp2p/js-libp2p-floodsub)
161+
- [ChainSafe/gossipsub-js](https://github.com/ChainSafe/gossipsub-js)
162+
163+
If none of the available pubsub routers fulfills your needs, you can create a libp2p compatible one. A libp2p pubsub router just needs to be created on top of [libp2p/js-libp2p-pubsub](https://github.com/libp2p/js-libp2p-pubsub), which ensures `js-libp2p` API expectations.
164+
165+
If you want to know more about libp2p pubsub, you should read the following content:
166+
167+
- https://docs.libp2p.io/concepts/publish-subscribe
168+
- https://github.com/libp2p/specs/tree/master/pubsub
169+
170+
## Customizing libp2p
171+
172+
When [creating a libp2p node](./API.md#create), the modules needed should be specified as follows:
173+
174+
```js
175+
const modules = {
176+
transport: [],
177+
streamMuxer: [],
178+
connEncryption: [],
179+
contentRouting: [],
180+
peerRouting: [],
181+
peerDiscovery: [],
182+
dht: dhtImplementation,
183+
pubsub: pubsubImplementation
184+
}
185+
```
186+
187+
Moreover, the majority of the modules can be customized via option parameters. This way, it is also possible to provide this options through a `config` object. This config object should have the property name of each building block to configure, the same way as the modules specification.
188+
189+
Besides the `modules` and `config`, libp2p allows other internal options and configurations:
190+
- `datastore`: an instance of [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore/) modules.
191+
- This is used in modules such as the DHT. If it is not provided, `js-libp2p` will use an in memory datastore.
192+
- `peerInfo`: a previously created instance of [libp2p/js-peer-info](https://github.com/libp2p/js-peer-info).
193+
- This is particularly useful if you want to reuse the same `peer-id`, as well as for modules like `libp2p-delegated-content-routing`, which need a `peer-id` in their instantiation.
194+
195+
### Examples
196+
197+
**1) Basic setup**
198+
199+
```js
200+
// Creating a libp2p node with:
201+
// transport: websockets + tcp
202+
// stream-muxing: mplex
203+
// crypto-channel: secio
204+
// discovery: multicast-dns
205+
// dht: kad-dht
206+
// pubsub: gossipsub
207+
208+
const Libp2p = require('libp2p')
209+
const TCP = require('libp2p-tcp')
210+
const WS = require('libp2p-websockets')
211+
const MPLEX = require('libp2p-mplex')
212+
const SECIO = require('libp2p-secio')
213+
const MulticastDNS = require('libp2p-mdns')
214+
const DHT = require('libp2p-kad-dht')
215+
const GossipSub = require('libp2p-gossipsub')
216+
217+
const node = await Libp2p.create({
218+
modules: {
219+
transport: [
220+
TCP,
221+
new WS() // It can take instances too!
222+
],
223+
streamMuxer: [MPLEX],
224+
connEncryption: [SECIO],
225+
peerDiscovery: [MulticastDNS],
226+
dht: DHT,
227+
pubsub: GossipSub
228+
}
229+
})
230+
```
231+
232+
**2) Customizing Peer Discovery**
233+
234+
```js
235+
const Libp2p = require('libp2p')
236+
const TCP = require('libp2p-tcp')
237+
const MPLEX = require('libp2p-mplex')
238+
const SECIO = require('libp2p-secio')
239+
const MulticastDNS = require('libp2p-mdns')
240+
241+
const node = await Libp2p.create({
242+
modules: {
243+
transport: [TCP],
244+
streamMuxer: [MPLEX],
245+
connEncryption: [SECIO],
246+
peerDiscovery: [MulticastDNS]
247+
},
248+
config: {
249+
peerDiscovery: {
250+
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minPeers)
251+
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
252+
// The associated object, will be passed to the service when it is instantiated.
253+
[MulticastDNS.tag]: {
254+
interval: 1000,
255+
enabled: true
256+
}
257+
// .. other discovery module options.
258+
}
259+
}
260+
})
261+
```
262+
263+
**3) Customizing Pubsub**
264+
265+
```js
266+
const Libp2p = require('libp2p')
267+
const TCP = require('libp2p-tcp')
268+
const MPLEX = require('libp2p-mplex')
269+
const SECIO = require('libp2p-secio')
270+
const GossipSub = require('libp2p-gossipsub')
271+
272+
const node = await Libp2p.create({
273+
modules: {
274+
transport: [TCP],
275+
streamMuxer: [MPLEX],
276+
connEncryption: [SECIO],
277+
pubsub: GossipSub
278+
},
279+
config: {
280+
pubsub: { // The pubsub options (and defaults) can be found in the pubsub router documentation
281+
enabled: true,
282+
emitSelf: true, // whether the node should emit to self on publish
283+
signMessages: true, // if messages should be signed
284+
strictSigning: true // if message signing should be required
285+
}
286+
}
287+
})
288+
```
289+
290+
**4) Customizing DHT**
291+
292+
```js
293+
const Libp2p = require('libp2p')
294+
const TCP = require('libp2p-tcp')
295+
const MPLEX = require('libp2p-mplex')
296+
const SECIO = require('libp2p-secio')
297+
const DHT = require('libp2p-kad-dht')
298+
299+
const node = await Libp2p.create({
300+
modules: {
301+
transport: [TCP],
302+
streamMuxer: [MPLEX],
303+
connEncryption: [SECIO],
304+
dht: DHT
305+
},
306+
config: {
307+
dht: { // The DHT options (and defaults) can be found in its documentation
308+
kBucketSize: 20,
309+
enabled: true,
310+
randomWalk: {
311+
enabled: true, // Allows to disable discovery (enabled by default)
312+
interval: 300e3,
313+
timeout: 10e3
314+
}
315+
}
316+
}
317+
})
318+
```
319+
320+
**5) Setup with Content and Peer Routing**
321+
322+
```js
323+
const Libp2p = require('libp2p')
324+
const TCP = require('libp2p-tcp')
325+
const MPLEX = require('libp2p-mplex')
326+
const SECIO = require('libp2p-secio')
327+
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
328+
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
329+
const PeerInfo = require('peer-info')
330+
331+
// create a peerInfo
332+
const peerInfo = await PeerInfo.create()
333+
334+
const node = await Libp2p.create({
335+
modules: {
336+
transport: [TCP],
337+
streamMuxer: [MPLEX],
338+
connEncryption: [SECIO],
339+
contentRouting: [
340+
new DelegatedContentRouter(peerInfo.id)
341+
],
342+
peerRouting: [
343+
new DelegatedPeerRouter()
344+
],
345+
},
346+
peerInfo
347+
})
348+
```
349+
350+
**6) Setup with Relay**
351+
352+
```js
353+
const Libp2p = require('libp2p')
354+
const TCP = require('libp2p-tcp')
355+
const MPLEX = require('libp2p-mplex')
356+
const SECIO = require('libp2p-secio')
357+
358+
const node = await Libp2p.create({
359+
modules: {
360+
transport: [TCP],
361+
streamMuxer: [MPLEX],
362+
connEncryption: [SECIO]
363+
},
364+
relay: { // Circuit Relay options (this config is part of libp2p core configurations)
365+
enabled: true,
366+
hop: {
367+
enabled: true,
368+
active: true
369+
}
370+
},
371+
})
372+
```
373+
374+
## Configuration examples
375+
376+
As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration:
377+
378+
379+
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-nodejs.js) - libp2p configuration used by js-ipfs when running in Node.js
380+
- [libp2p-ipfs-browser](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-browser.js) - libp2p configuration used by js-ipfs when running in a Browser (that supports WebRTC)
381+
382+
If you have developed a project using `js-libp2p`, please consider submitting your configuration to this list so that it can be found easily by other users.
383+
384+
The [examples](../examples) are also a good source of help for finding a configuration that suits your needs.

0 commit comments

Comments
 (0)