The JavaScript implementation of the libp2p Networking Stack.
Project status
We've come a long way, but this project is still in Alpha, lots of development is happening, API might change, beware of the Dragons 🐉..
Want to get started? Check our examples folder.
Weekly Core Dev Calls
Tech Lead
David Dias
Lead Maintainer
Jacob Heun
Table of Contents
Background
libp2p is the product of a long and arduous quest to understand the evolution of the Internet networking stack. In order to build P2P applications, devs have long had to make custom ad-hoc solutions to fit their needs, sometimes making some hard assumptions about their runtimes and the state of the network at the time of their development. Today, looking back more than 20 years, we see a clear pattern in the types of mechanisms built around the Internet Protocol, IP, which can be found throughout many layers of the OSI layer system, libp2p distils these mechanisms into flat categories and defines clear interfaces that once exposed, enable other protocols and applications to use and swap them, enabling upgradability and adaptability for the runtime, without breaking the API.
We are in the process of writing better documentation, blog posts, tutorials and a formal specification. Today you can find:
To sum up, libp2p is a "network stack" -- a protocol suite -- that cleanly separates concerns, and enables sophisticated applications to only use the protocols they absolutely need, without giving up interoperability and upgradeability. libp2p grew out of IPFS, but it is built so that lots of people can use it, for lots of different projects.
Bundles
With its modular nature, libp2p can be found being used in different projects with different sets of features, while preserving the same top level API. js-libp2p
is only a skeleton and should not be installed directly, if you are looking for a prebundled libp2p stack, please check:
If you have developed a libp2p bundle, please consider submitting it to this list so that it can be found easily by the users of libp2p.
Install
Again, as noted above, this module is only a skeleton and should not be used directly other than libp2p bundle implementors that want to extend its code.
npm install --save libp2p
Usage
IMPORTANT NOTE: We are currently on the way of migrating all our libp2p
modules to use async await
and async iterators
, instead of callbacks and pull-streams
. As a consequence, when you start a new libp2p project, we must check which versions of the modules you should use. For now, it is required to use the modules using callbacks with libp2p
, while we are working on getting the remaining modules ready for a full migration. For more details, you can have a look at libp2p/js-libp2p#266.
You can find multiple examples on the examples folder that will guide you through using libp2p for several scenarios.
Creating your own libp2p bundle
The libp2p module acts as a glue for every libp2p module that you can use to create your own libp2p bundle. Creating your own libp2p bundle gives you a lot of freedom when it comes to customize it with features and default setup. We recommend creating your own libp2p bundle for the app you are developing that takes into account your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have built one that leverages the Browser transports).
Example:
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const SPDY = require('libp2p-spdy')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const MulticastDNS = require('libp2p-mdns')
const DHT = require('libp2p-kad-dht')
const GossipSub = require('libp2p-gossipsub')
const defaultsDeep = require('@nodeutils/defaults-deep')
const Protector = require('libp2p/src/pnet')
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
class Node extends Libp2p {
constructor (_options) {
const peerInfo = _options.peerInfo
const defaults = {
modules: {
transport: [
TCP,
new WS()
],
streamMuxer: [
SPDY,
MPLEX
],
connEncryption: [
SECIO
],
peerDiscovery: [
MulticastDNS
],
dht: DHT,
pubsub: GossipSub
},
config: {
peerDiscovery: {
autoDial: true,
mdns: {
interval: 1000,
enabled: true
},
webrtcStar: {
interval: 1000,
enabled: false
}
},
relay: {
enabled: true,
hop: {
enabled: false,
active: false
}
},
dht: {
kBucketSize: 20,
enabled: true,
randomWalk: {
enabled: true,
interval: 300e3,
timeout: 10e3
}
},
pubsub: {
enabled: true,
emitSelf: true,
signMessages: true,
strictSigning: true
}
}
}
super(defaultsDeep(_options, defaults))
}
}
API
See API.md.
Development
Clone and install dependencies:
> git clone https://github.com/libp2p/js-libp2p.git
> cd js-libp2p
> npm install
Tests
Run unit tests
> npm test
> npm run test:node
> npm run test:browser
Packages
List of packages currently in existence for libp2p
This table is generated using the module package-table
with package-table --data=package-list.json
.
Contribute
The libp2p implementation in JavaScript is a work in progress. As such, there are a few things you can do right now to help out:
- Go through the modules and check out existing issues. This would be especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
- Perform code reviews. Most of this has been developed by @diasdavid, which means that more eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
- Add tests. There can never be enough tests.
License
MIT © Protocol Labs