* docs: new api * chore: new iteration * chore: apply suggestions from code review Co-Authored-By: Alan Shaw <alan.shaw@protocol.ai> * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * chore: address review * docs: add events * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
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.
Tech Lead
Lead Maintainer
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:
- libp2p.io
- docs.libp2p.io
- Specification (WIP)
- Discussion Forums
- Talks
- Articles
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:
- libp2p-ipfs-nodejs - The libp2p build used by js-ipfs when run in Node.js
- libp2p-ipfs-browser - The libp2p build used by js-ipfs when run in a Browser (that supports WebRTC)
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.
Tutorials and Examples
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:
// Creating a bundle that adds:
// transport: websockets + tcp
// stream-muxing: spdy & mplex
// crypto-channel: secio
// discovery: multicast-dns
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 = {
// The libp2p modules for this libp2p bundle
modules: {
transport: [
TCP,
new WS() // It can take instances too!
],
streamMuxer: [
SPDY,
MPLEX
],
connEncryption: [
SECIO
],
/** Encryption for private networks. Needs additional private key to work **/
// connProtector: new Protector(/*protector specific opts*/),
/** Enable custom content routers, such as delegated routing **/
// contentRouting: [
// new DelegatedContentRouter(peerInfo.id)
// ],
/** Enable custom peer routers, such as delegated routing **/
// peerRouting: [
// new DelegatedPeerRouter()
// ],
peerDiscovery: [
MulticastDNS
],
dht: DHT, // DHT enables PeerRouting, ContentRouting and DHT itself components
pubsub: GossipSub
},
// libp2p config options (typically found on a config.json)
config: { // The config object is the part of the config that can go into a file, config.json.
peerDiscovery: {
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minPeers)
mdns: { // mdns options
interval: 1000, // ms
enabled: true
},
webrtcStar: { // webrtc-star options
interval: 1000, // ms
enabled: false
}
// .. other discovery module options.
},
relay: { // Circuit Relay options
enabled: true,
hop: {
enabled: false,
active: false
}
},
dht: {
kBucketSize: 20,
enabled: true,
randomWalk: {
enabled: true, // Allows to disable discovery (enabled by default)
interval: 300e3,
timeout: 10e3
}
},
pubsub: {
enabled: true,
emitSelf: true, // whether the node should emit to self on publish, in the event of the topic being subscribed
signMessages: true, // if messages should be signed
strictSigning: true // if message signing should be required
}
}
}
// overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep
super(defaultsDeep(_options, defaults))
}
}
// Now all the nodes you create, will have TCP, WebSockets, SPDY, MPLEX, SECIO and MulticastDNS support.
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
# run all the unit tsts
> npm test
# run just Node.js tests
> npm run test:node
# run just Browser tests (Chrome)
> npm run test:browser
Packages
List of packages currently in existence for libp2p
This table is generated using the module
package-table
withpackage-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