2017-07-05 18:18:31 +01:00
< h1 align = "center" >
2018-07-06 19:16:21 +02:00
< a href = "libp2p.io" > < img width = "250" src = "https://github.com/libp2p/libp2p/blob/master/logo/black-bg-2.png?raw=true" alt = "libp2p hex logo" / > < / a >
2017-07-05 18:18:31 +01:00
< / h1 >
2016-09-02 15:22:28 -04:00
2017-07-05 18:18:31 +01:00
< h3 align = "center" > The JavaScript implementation of the libp2p Networking Stack.< / h3 >
2015-08-25 11:00:19 +01:00
2017-07-05 18:18:31 +01:00
< p align = "center" >
< a href = "http://ipn.io" > < img src = "https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square" / > < / a >
2018-05-29 13:45:48 +01:00
< a href = "http://libp2p.io/" > < img src = "https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square" / > < / a >
< a href = "http://webchat.freenode.net/?channels=%23libp2p" > < img src = "https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square" / > < / a >
2019-09-23 11:39:47 +02:00
< a href = "https://riot.permaweb.io/ #/room/#libp2p:permaweb .io" >< img src = "https://img.shields.io/badge/matrix-%23libp2p%3Apermaweb.io-blue.svg?style=flat-square" /> </ a >
< a href = "https://discord.gg/66KBrm2" > < img src = "https://img.shields.io/discord/475789330380488707?color=blueviolet&label=discord&style=flat-square" / > < / a >
2019-04-10 10:53:19 +01:00
< a href = "https://discuss.libp2p.io" > < img src = "https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg" / > < / a >
2017-07-05 18:18:31 +01:00
< / p >
< p align = "center" >
2019-02-21 16:07:35 +00:00
< a href = "https://travis-ci.com/libp2p/js-libp2p" > < img src = "https://flat.badgen.net/travis/libp2p/js-libp2p" / > < / a >
< a href = "https://codecov.io/gh/libp2p/js-libp2p" > < img src = "https://img.shields.io/codecov/c/github/ipfs/js-ipfs-multipart/master.svg?style=flat-square" > < / a >
< a href = "https://bundlephobia.com/result?p=ipfsd-ctl" > < img src = "https://flat.badgen.net/bundlephobia/minzip/ipfsd-ctl" > < / a >
2017-07-05 18:18:31 +01:00
< br >
< a href = "https://david-dm.org/libp2p/js-libp2p" > < img src = "https://david-dm.org/libp2p/js-libp2p.svg?style=flat-square" / > < / a >
< a href = "https://github.com/feross/standard" > < img src = "https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square" > < / a >
< a href = "https://github.com/RichardLitt/standard-readme" > < img src = "https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square" / > < / a >
< a href = "" > < img src = "https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square" / > < / a >
2017-09-03 14:39:55 +01:00
< a href = "" > < img src = "https://img.shields.io/badge/Node.js-%3E%3D6.0.0-orange.svg?style=flat-square" / > < / a >
2017-07-05 18:18:31 +01:00
< br >
< / p >
2016-11-25 20:07:45 +00:00
2017-07-23 11:20:18 -07:00
### 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 🐉..
2019-08-27 17:07:21 +02:00
**Want to get started?** Check our [examples folder ](/examples ).
2017-07-23 11:20:18 -07:00
2018-06-22 00:05:53 +01:00
[**`Weekly Core Dev Calls`** ](https://github.com/ipfs/pm/issues/650 )
2018-05-06 18:32:26 +02:00
## Tech Lead
[David Dias ](https://github.com/diasdavid/ )
## Lead Maintainer
2018-07-27 15:59:52 +02:00
[Jacob Heun ](https://github.com/jacobheun/ )
2018-05-06 18:32:26 +02:00
2016-09-02 15:22:28 -04:00
## Table of Contents
2015-11-18 16:28:20 +00:00
2016-09-02 15:22:28 -04:00
- [Background ](#background )
2016-11-25 20:07:45 +00:00
- [Bundles ](#bundles )
2016-09-02 15:22:28 -04:00
- [Usage ](#usage )
2016-11-25 20:07:45 +00:00
- [Install ](#install )
2017-07-06 14:26:20 +01:00
- [Usage ](#usage )
2016-11-25 20:07:45 +00:00
- [API ](#api )
2018-10-19 17:37:34 +02:00
- [Events ](#events )
2016-11-25 20:07:45 +00:00
- [Development ](#development )
- [Tests ](#tests )
- [Packages ](#packages )
2016-09-02 15:22:28 -04:00
- [Contribute ](#contribute )
- [License ](#license )
2016-11-25 20:07:45 +00:00
## Background
2016-09-14 00:36:03 -04:00
2017-12-11 07:16:05 -05:00
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 made 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.
2016-09-14 00:36:03 -04:00
2017-09-06 10:55:12 +02:00
We are in the process of writing better documentation, blog posts, tutorials and a formal specification. Today you can find:
2016-09-14 00:36:03 -04:00
2017-07-04 11:43:45 +01:00
- [libp2p.io ](https://libp2p.io )
2019-04-04 04:49:40 -04:00
- [docs.libp2p.io ](https://docs.libp2p.io )
2016-11-27 18:54:30 +00:00
- [Specification (WIP) ](https://github.com/libp2p/specs )
2019-04-04 04:49:40 -04:00
- [Discussion Forums ](https://discuss.libp2p.io )
2016-11-25 20:07:45 +00:00
- Talks
2017-07-06 14:26:20 +01:00
- [`libp2p <3 ethereum` at DEVCON2 ](https://ethereumfoundation.org/devcon/?session=libp2p ) [📼 video ](https://www.youtube.com/watch?v=HxueJbeMVG4 ) [slides ](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p-HEART-devp2p-IPFS-PLUS-Ethereum-networking.pdf ) [📼 demo-1 ](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo1-1.mp4 ) [📼 demo-2 ](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo2-1.mp4 )
2016-11-25 20:07:45 +00:00
- Articles
2016-11-27 18:54:30 +00:00
- [The overview of libp2p ](https://github.com/libp2p/libp2p#description )
2016-09-14 01:31:47 -04:00
2016-11-25 20:07:45 +00:00
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.
2015-11-18 16:28:20 +00:00
2016-11-25 20:07:45 +00:00
## Bundles
2015-11-18 16:28:20 +00:00
2017-01-16 19:01:09 +08:00
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:
2015-11-18 16:28:20 +00:00
2017-07-04 11:43:45 +01:00
- [libp2p-ipfs-nodejs ](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-nodejs.js ) - The libp2p build used by js-ipfs when run in Node.js
- [libp2p-ipfs-browser ](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/libp2p-browser.js ) - The libp2p build used by js-ipfs when run in a Browser (that supports WebRTC)
2015-11-18 16:28:20 +00:00
2016-11-25 20:07:45 +00:00
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.
2015-11-18 16:28:20 +00:00
2016-09-02 15:22:28 -04:00
## Install
2015-09-20 16:18:34 +01:00
2017-01-16 19:01:09 +08:00
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.
2016-11-25 20:07:45 +00:00
2016-09-02 15:22:28 -04:00
```sh
2016-09-14 00:36:03 -04:00
npm install --save libp2p
2016-09-02 15:22:28 -04:00
```
## Usage
2015-09-20 16:18:34 +01:00
2019-08-06 12:01:16 +02:00
**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 ](https://github.com/libp2p/js-libp2p/issues/266 ).
2017-07-06 14:26:20 +01:00
### [Tutorials and Examples](/examples)
2017-09-06 10:55:12 +02:00
You can find multiple examples on the [examples folder ](/examples ) that will guide you through using libp2p for several scenarios.
2017-07-06 14:26:20 +01:00
2018-08-22 09:12:58 +02:00
### Creating your own libp2p bundle
2015-09-20 16:18:34 +01:00
2019-03-12 13:25:24 +05:30
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 in account your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have a built one that leverages the Browser transports).
2015-09-27 00:14:40 +01:00
2017-03-24 16:02:57 +00:00
**Example:**
```JavaScript
// Creating a bundle that adds:
// transport: websockets + tcp
2018-06-28 10:06:25 +02:00
// stream-muxing: spdy & mplex
2017-03-24 16:02:57 +00:00
// crypto-channel: secio
// discovery: multicast-dns
2019-06-07 15:50:23 +02:00
const Libp2p = require('libp2p')
2017-03-24 16:02:57 +00:00
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
2018-06-28 10:06:25 +02:00
const SPDY = require('libp2p-spdy')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
2017-03-24 16:02:57 +00:00
const MulticastDNS = require('libp2p-mdns')
2017-04-06 15:45:23 -04:00
const DHT = require('libp2p-kad-dht')
2019-07-31 09:38:14 +02:00
const GossipSub = require('libp2p-gossipsub')
2018-06-28 10:06:25 +02:00
const defaultsDeep = require('@nodeutils/defaults -deep')
2019-10-16 17:27:31 +02:00
const Protector = require('libp2p/src/pnet')
2018-10-19 16:28:28 +02:00
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
2017-03-24 16:02:57 +00:00
2019-06-07 15:50:23 +02:00
class Node extends Libp2p {
2018-08-14 14:17:18 +02:00
constructor (_options) {
2018-10-19 16:28:28 +02:00
const peerInfo = _options.peerInfo
2018-06-28 10:06:25 +02:00
const defaults = {
// The libp2p modules for this libp2p bundle
modules: {
transport: [
TCP,
new WS() // It can take instances too!
2017-03-24 16:02:57 +00:00
],
2018-06-28 10:06:25 +02:00
streamMuxer: [
SPDY,
MPLEX
],
connEncryption: [
SECIO
2018-07-05 11:47:00 +02:00
],
2018-10-19 16:28:28 +02:00
/** Encryption for private networks. Needs additional private key to work ** /
2018-08-14 14:17:18 +02:00
// connProtector: new Protector(/*protector specific opts*/),
2018-10-19 16:28:28 +02:00
/** Enable custom content routers, such as delegated routing ** /
// contentRouting: [
// new DelegatedContentRouter(peerInfo.id)
// ],
/** Enable custom peer routers, such as delegated routing ** /
// peerRouting: [
// new DelegatedPeerRouter()
// ],
2018-06-28 10:06:25 +02:00
peerDiscovery: [
MulticastDNS
],
2019-07-31 09:38:14 +02:00
dht: DHT, // DHT enables PeerRouting, ContentRouting and DHT itself components
pubsub: GossipSub
2018-06-28 10:06:25 +02:00
},
// 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: {
2019-04-11 12:44:58 +02:00
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minPeers)
2018-06-28 10:06:25 +02:00
mdns: { // mdns options
2018-08-13 17:10:40 +02:00
interval: 1000, // ms
2018-06-28 10:06:25 +02:00
enabled: true
},
webrtcStar: { // webrtc-star options
2018-08-13 17:10:40 +02:00
interval: 1000, // ms
2018-06-28 10:06:25 +02:00
enabled: false
}
// .. other discovery module options.
},
relay: { // Circuit Relay options
2018-10-19 16:31:40 +02:00
enabled: true,
2018-06-28 10:06:25 +02:00
hop: {
enabled: false,
active: false
}
2018-07-05 11:47:00 +02:00
},
2018-07-19 15:53:18 +02:00
dht: {
2018-10-04 13:40:32 +01:00
kBucketSize: 20,
2019-01-29 17:57:09 +00:00
enabled: true,
2019-04-16 15:04:23 +02:00
randomWalk: {
enabled: true, // Allows to disable discovery (enabled by default)
interval: 300e3,
timeout: 10e3
}
2018-07-19 15:53:18 +02:00
},
2019-07-31 09:38:14 +02:00
pubsub: {
2019-08-19 17:06:08 +02:00
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
2018-06-28 10:06:25 +02:00
}
2018-07-05 11:47:00 +02:00
}
2017-03-24 16:02:57 +00:00
}
2018-06-28 10:06:25 +02:00
// overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep
super(defaultsDeep(_options, defaults))
2017-03-24 16:02:57 +00:00
}
}
2018-06-28 10:06:25 +02:00
// Now all the nodes you create, will have TCP, WebSockets, SPDY, MPLEX, SECIO and MulticastDNS support.
2017-03-24 16:02:57 +00:00
```
2016-11-25 20:07:45 +00:00
2017-03-24 16:02:57 +00:00
### API
2016-11-25 20:07:45 +00:00
2019-07-29 14:40:40 +01:00
**IMPORTANT NOTE**: All the methods listed in the API section that take a callback are also now Promisified. Libp2p is migrating away from callbacks to async/await, and in a future release (that will be announced in advance), callback support will be removed entirely. You can follow progress of the async/await endeavor at https://github.com/ipfs/js-ipfs/issues/1670.
2019-06-07 15:50:23 +02:00
#### Create a Node - `Libp2p.createLibp2p(options, callback)`
2017-03-24 18:20:14 +00:00
2019-06-07 15:50:23 +02:00
> Behaves exactly like `new Libp2p(options)`, but doesn't require a PeerInfo. One will be generated instead
```js
const { createLibp2p } = require('libp2p')
createLibp2p(options, (err, libp2p) => {
if (err) throw err
libp2p.start((err) => {
if (err) throw err
})
})
```
- `options` : Object of libp2p configuration options
- `callback` : Function with signature `function (Error, Libp2p) {}`
#### Create a Node alternative - `new Libp2p(options)`
> Creates an instance of Libp2p with a custom `PeerInfo` provided via `options.peerInfo`.
2017-03-24 18:20:14 +00:00
2018-08-14 14:23:55 +02:00
Required keys in the `options` object:
- `peerInfo` : instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node.
2019-06-07 15:50:23 +02:00
- `modules.transport` : An array that must include at least 1 transport, such as `libp2p-tcp` .
2016-11-25 20:07:45 +00:00
2017-03-24 16:02:57 +00:00
#### `libp2p.start(callback)`
2017-03-24 18:20:14 +00:00
> Start the libp2p Node.
2018-10-19 17:37:34 +02:00
`callback` following signature `function (err) {}` , where `err` is an Error in case starting the node fails.
2017-03-24 18:20:14 +00:00
2017-03-24 16:02:57 +00:00
#### `libp2p.stop(callback)`
2017-03-24 18:20:14 +00:00
> Stop the libp2p Node.
2018-10-19 17:37:34 +02:00
`callback` following signature `function (err) {}` , where `err` is an Error in case stopping the node fails.
2017-03-24 18:20:14 +00:00
2018-02-07 07:48:50 +00:00
#### `libp2p.dial(peer, callback)`
2017-03-24 16:02:57 +00:00
2018-02-07 07:48:50 +00:00
> Dials to another peer in the network, establishes the connection.
- `peer` : can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
2018-10-19 17:37:34 +02:00
- `callback` following signature `function (err, conn) {}` , where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
2018-02-07 07:48:50 +00:00
#### `libp2p.dialProtocol(peer, protocol, callback)`
> Dials to another peer in the network and selects a protocol to talk with that peer.
2017-03-24 18:20:14 +00:00
2018-02-05 03:36:20 -08:00
- `peer` : can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
2017-03-24 18:20:14 +00:00
- `protocol` : String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
2018-10-19 17:37:34 +02:00
- `callback` : Function with signature `function (err, conn) {}` , where `conn` is a [Connection ](https://github.com/libp2p/interface-connection ) object
`callback` following signature `function (err, conn) {}` , where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
2017-03-24 18:20:14 +00:00
2018-10-19 17:37:34 +02:00
#### `libp2p.dialFSM(peer, protocol, callback)`
> Behaves like `.dial` and `.dialProtocol` but calls back with a Connection State Machine
- `peer` : can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
- `protocol` : an optional String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
- `callback` : following signature `function (err, connFSM) {}` , where `connFSM` is a [Connection State Machine ](https://github.com/libp2p/js-libp2p-switch#connection-state-machine )
2017-03-24 18:20:14 +00:00
2018-01-02 07:18:41 +00:00
#### `libp2p.hangUp(peer, callback)`
2017-03-24 16:02:57 +00:00
2017-03-24 18:20:14 +00:00
> Closes an open connection with a peer, graciously.
- `peer` : can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
2018-10-19 17:37:34 +02:00
`callback` following signature `function (err) {}` , where `err` is an Error in case stopping the node fails.
2017-03-24 18:20:14 +00:00
2018-10-19 16:28:28 +02:00
#### `libp2p.peerRouting.findPeer(id, options, callback)`
2017-04-06 15:45:23 -04:00
> Looks up for multiaddrs of a peer in the DHT
- `id` : instance of [PeerId][]
2018-10-19 16:28:28 +02:00
- `options` : object of options
- `options.maxTimeout` : Number milliseconds
2017-04-06 15:45:23 -04:00
2018-10-19 16:28:28 +02:00
#### `libp2p.contentRouting.findProviders(key, options, callback)`
2017-04-06 15:45:23 -04:00
- `key` : Buffer
2018-10-19 16:28:28 +02:00
- `options` : object of options
- `options.maxTimeout` : Number milliseconds
2018-11-13 10:46:51 +00:00
- `options.maxNumProviders` maximum number of providers to find
2017-04-06 15:45:23 -04:00
2017-04-16 11:56:34 +01:00
#### `libp2p.contentRouting.provide(key, callback)`
2017-04-06 15:45:23 -04:00
- `key` : Buffer
2017-03-24 16:02:57 +00:00
#### `libp2p.handle(protocol, handlerFunc [, matchFunc])`
2017-03-24 18:20:14 +00:00
> Handle new protocol
- `protocol` : String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
2018-10-19 17:37:34 +02:00
- `handlerFunc` : following signature `function (protocol, conn) {}` , where `conn` is a [Connection ](https://github.com/libp2p/interface-connection ) object
2017-03-24 18:20:14 +00:00
- `matchFunc` : Function for matching on protocol (exact matching, semver, etc). Default to exact match.
2017-09-06 10:55:12 +02:00
#### `libp2p.unhandle(protocol)`
2017-03-24 16:02:57 +00:00
2017-03-24 18:20:14 +00:00
> Stop handling protocol
- `protocol` : String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
2018-10-19 17:37:34 +02:00
#### Events
##### `libp2p.on('start', () => {})`
> Libp2p has started, along with all its services.
##### `libp2p.on('stop', () => {})`
> Libp2p has stopped, along with all its services.
##### `libp2p.on('error', (err) => {})`
> An error has occurred
- `err` : instance of `Error`
##### `libp2p.on('peer:discovery', (peer) => {})`
2017-03-24 16:02:57 +00:00
2017-03-24 18:20:14 +00:00
> Peer has been discovered.
2019-04-11 12:44:58 +02:00
If `autoDial` is `true` , applications should **not** attempt to connect to the peer
unless they are performing a specific action. See [peer discovery and auto dial ](./PEER_DISCOVERY.md ) for more information.
2017-03-24 18:20:14 +00:00
- `peer` : instance of [PeerInfo][]
2018-10-19 17:37:34 +02:00
##### `libp2p.on('peer:connect', (peer) => {})`
2017-03-29 07:32:29 +01:00
2019-04-11 13:49:31 +02:00
> We have a new muxed connection to a peer
2017-03-29 07:32:29 +01:00
- `peer` : instance of [PeerInfo][]
2018-10-19 17:37:34 +02:00
##### `libp2p.on('peer:disconnect', (peer) => {})`
2017-03-29 07:32:29 +01:00
2019-04-11 13:49:31 +02:00
> We have closed a connection to a peer
- `peer` : instance of [PeerInfo][]
##### `libp2p.on('connection:start', (peer) => {})`
> We created a new connection to a peer
- `peer` : instance of [PeerInfo][]
##### `libp2p.on('connection:end', (peer) => {})`
> We closed a connection to a peer
2017-03-29 07:32:29 +01:00
- `peer` : instance of [PeerInfo][]
2017-07-09 11:42:11 +01:00
#### `libp2p.isStarted()`
2017-03-24 16:02:57 +00:00
2017-03-24 18:20:14 +00:00
> Check if libp2p is started
2017-03-27 12:26:34 +01:00
#### `libp2p.ping(peer [, options], callback)`
> Ping a node in the network
2017-03-24 18:20:14 +00:00
#### `libp2p.peerBook`
> PeerBook instance of the node
#### `libp2p.peerInfo`
> PeerInfo instance of the node
2016-11-25 20:07:45 +00:00
2018-02-15 19:39:06 +01:00
#### `libp2p.pubsub`
> Same API as IPFS PubSub, defined in the [CORE API Spec](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/PUBSUB.md). Just replace `ipfs` by `libp2p` and you are golden.
2017-03-24 17:07:08 +00:00
---------------------
2018-02-15 19:39:06 +01:00
`DHT methods also exposed for the time being`
2017-07-09 11:42:11 +01:00
#### `libp2p.dht.put(key, value, callback)`
- `key` : Buffer
- `value` : Buffer
2018-10-19 16:28:28 +02:00
#### `libp2p.dht.get(key, options, callback)`
2017-07-09 11:42:11 +01:00
- `key` : Buffer
2018-10-19 16:28:28 +02:00
- `options` : object of options
- `options.maxTimeout` : Number milliseconds
2017-07-09 11:42:11 +01:00
2018-10-19 16:28:28 +02:00
#### `libp2p.dht.getMany(key, nVals, options, callback)`
2017-07-09 11:42:11 +01:00
- `key` : Buffer
- `nVals` : Number
2018-10-19 16:28:28 +02:00
- `options` : object of options
- `options.maxTimeout` : Number milliseconds
2017-07-09 11:42:11 +01:00
2017-03-24 18:20:14 +00:00
[PeerInfo]: https://github.com/libp2p/js-peer-info
[PeerId]: https://github.com/libp2p/js-peer-id
[PeerBook]: https://github.com/libp2p/js-peer-book
[multiaddr]: https://github.com/multiformats/js-multiaddr
[Connection]: https://github.com/libp2p/interface-connection
2018-02-23 13:19:39 +00:00
-------
### Switch Stats API
##### `libp2p.stats.emit('update')`
Every time any stat value changes, this object emits an `update` event.
#### Global stats
##### `libp2p.stats.global.snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big ](https://github.com/MikeMcl/big.js#readme ) number
- dataReceived: amount of bytes received, [Big ](https://github.com/MikeMcl/big.js#readme ) number
##### `libp2p.stats.global.movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000` , `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance ](https://github.com/pgte/moving-average#readme ).
#### Per-transport stats
##### `libp2p.stats.transports()`
Returns an array containing the tags (string) for each observed transport.
##### `libp2p.stats.forTransport(transportTag).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big ](https://github.com/MikeMcl/big.js#readme ) number
- dataReceived: amount of bytes received, [Big ](https://github.com/MikeMcl/big.js#readme ) number
##### `libp2p.stats.forTransport(transportTag).movingAverages`
Returns an object containing the following keys:
dataSent
dataReceived
Each one of them contains an object that has a key for each interval (`60000` , `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance ](https://github.com/pgte/moving-average#readme ).
#### Per-protocol stats
##### `libp2p.stats.protocols()`
Returns an array containing the tags (string) for each observed protocol.
##### `libp2p.stats.forProtocol(protocolTag).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big ](https://github.com/MikeMcl/big.js#readme ) number
- dataReceived: amount of bytes received, [Big ](https://github.com/MikeMcl/big.js#readme ) number
##### `libp2p.stats.forProtocol(protocolTag).movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000` , `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance ](https://github.com/pgte/moving-average#readme ).
#### Per-peer stats
##### `libp2p.stats.peers()`
Returns an array containing the peerIDs (B58-encoded string) for each observed peer.
##### `libp2p.stats.forPeer(peerId:String).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big ](https://github.com/MikeMcl/big.js#readme ) number
- dataReceived: amount of bytes received, [Big ](https://github.com/MikeMcl/big.js#readme ) number
##### `libp2p.stats.forPeer(peerId:String).movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000` , `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance ](https://github.com/pgte/moving-average#readme ).
#### Stats update interval
Stats are not updated in real-time. Instead, measurements are buffered and stats are updated at an interval. The maximum interval can be defined through the `Switch` constructor option `stats.computeThrottleTimeout` , defined in miliseconds.
2018-06-08 00:30:21 +02:00
### Private Networks
#### Enforcement
Libp2p provides support for connection protection, such as for private networks. You can enforce network protection by setting the environment variable `LIBP2P_FORCE_PNET=1` . When this variable is on, if no protector is set via `options.connProtector` , Libp2p will throw an error upon creation.
#### Protectors
Some available network protectors:
2019-10-16 17:27:31 +02:00
* [libp2p-pnet ](https://github.com/libp2p/js-libp2p/tree/master/src/pnet )
2018-02-23 13:19:39 +00:00
2017-10-22 10:27:36 +01:00
## Development
**Clone and install dependencies:**
```sh
2019-10-16 17:27:31 +02:00
> git clone https://github.com/libp2p/js-libp2p.git
> cd js-libp2p
2017-10-22 10:27:36 +01:00
> npm install
```
### Tests
#### Run unit tests
```sh
# 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
```
2016-11-25 20:07:45 +00:00
### Packages
2017-01-16 19:01:09 +08:00
List of packages currently in existence for libp2p
2016-11-25 20:07:45 +00:00
2018-08-22 11:50:21 +02:00
> This table is generated using the module `package-table` with `package-table --data=package-list.json`.
2018-10-07 08:59:20 +01:00
| Package | Version | Deps | CI | Coverage | Lead Maintainer |
| ---------|---------|---------|---------|---------|--------- |
2018-07-05 17:17:47 +02:00
| **Libp2p** |
2019-03-15 15:18:27 +00:00
| [`interface-libp2p` ](//github.com/libp2p/interface-libp2p ) | [](//github.com/libp2p/interface-libp2p/releases) | [](https://david-dm.org/libp2p/interface-libp2p) | [](https://travis-ci.com/libp2p/interface-libp2p) | [](https://codecov.io/gh/libp2p/interface-libp2p) | N/A |
| [`libp2p` ](//github.com/libp2p/js-libp2p ) | [](//github.com/libp2p/js-libp2p/releases) | [](https://david-dm.org/libp2p/js-libp2p) | [](https://travis-ci.com/libp2p/js-libp2p) | [](https://codecov.io/gh/libp2p/js-libp2p) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
2018-07-05 17:17:47 +02:00
| **Connection** |
2019-03-15 15:18:27 +00:00
| [`interface-connection` ](//github.com/libp2p/interface-connection ) | [](//github.com/libp2p/interface-connection/releases) | [](https://david-dm.org/libp2p/interface-connection) | [](https://travis-ci.com/libp2p/interface-connection) | [](https://codecov.io/gh/libp2p/interface-connection) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
2018-07-05 17:17:47 +02:00
| **Transport** |
2019-03-15 15:18:27 +00:00
| [`interface-transport` ](//github.com/libp2p/interface-transport ) | [](//github.com/libp2p/interface-transport/releases) | [](https://david-dm.org/libp2p/interface-transport) | [](https://travis-ci.com/libp2p/interface-transport) | [](https://codecov.io/gh/libp2p/interface-transport) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
| [`libp2p-tcp` ](//github.com/libp2p/js-libp2p-tcp ) | [](//github.com/libp2p/js-libp2p-tcp/releases) | [](https://david-dm.org/libp2p/js-libp2p-tcp) | [](https://travis-ci.com/libp2p/js-libp2p-tcp) | [](https://codecov.io/gh/libp2p/js-libp2p-tcp) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
| [`libp2p-utp` ](//github.com/libp2p/js-libp2p-utp ) | [](//github.com/libp2p/js-libp2p-utp/releases) | [](https://david-dm.org/libp2p/js-libp2p-utp) | [](https://travis-ci.com/libp2p/js-libp2p-utp) | [](https://codecov.io/gh/libp2p/js-libp2p-utp) | N/A |
| [`libp2p-webrtc-direct` ](//github.com/libp2p/js-libp2p-webrtc-direct ) | [](//github.com/libp2p/js-libp2p-webrtc-direct/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-direct) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-direct) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-direct) | [Vasco Santos ](mailto:vasco.santos@moxy.studio ) |
| [`libp2p-webrtc-star` ](//github.com/libp2p/js-libp2p-webrtc-star ) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos ](mailto:vasco.santos@moxy.studio ) |
| [`libp2p-websockets` ](//github.com/libp2p/js-libp2p-websockets ) | [](//github.com/libp2p/js-libp2p-websockets/releases) | [](https://david-dm.org/libp2p/js-libp2p-websockets) | [](https://travis-ci.com/libp2p/js-libp2p-websockets) | [](https://codecov.io/gh/libp2p/js-libp2p-websockets) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
| [`libp2p-websocket-star` ](//github.com/libp2p/js-libp2p-websocket-star ) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [](https://travis-ci.com/libp2p/js-libp2p-websocket-star) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
2018-07-05 17:17:47 +02:00
| **Crypto Channels** |
2019-03-15 15:18:27 +00:00
| [`libp2p-secio` ](//github.com/libp2p/js-libp2p-secio ) | [](//github.com/libp2p/js-libp2p-secio/releases) | [](https://david-dm.org/libp2p/js-libp2p-secio) | [](https://travis-ci.com/libp2p/js-libp2p-secio) | [](https://codecov.io/gh/libp2p/js-libp2p-secio) | [Friedel Ziegelmayer ](mailto:dignifiedquire@gmail.com ) |
2018-07-05 17:17:47 +02:00
| **Stream Muxers** |
2019-03-15 15:18:27 +00:00
| [`interface-stream-muxer` ](//github.com/libp2p/interface-stream-muxer ) | [](//github.com/libp2p/interface-stream-muxer/releases) | [](https://david-dm.org/libp2p/interface-stream-muxer) | [](https://travis-ci.com/libp2p/interface-stream-muxer) | [](https://codecov.io/gh/libp2p/interface-stream-muxer) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
| [`libp2p-mplex` ](//github.com/libp2p/js-libp2p-mplex ) | [](//github.com/libp2p/js-libp2p-mplex/releases) | [](https://david-dm.org/libp2p/js-libp2p-mplex) | [](https://travis-ci.com/libp2p/js-libp2p-mplex) | [](https://codecov.io/gh/libp2p/js-libp2p-mplex) | [Vasco Santos ](mailto:vasco.santos@moxy.studio ) |
| [`libp2p-spdy` ](//github.com/libp2p/js-libp2p-spdy ) | [](//github.com/libp2p/js-libp2p-spdy/releases) | [](https://david-dm.org/libp2p/js-libp2p-spdy) | [](https://travis-ci.com/libp2p/js-libp2p-spdy) | [](https://codecov.io/gh/libp2p/js-libp2p-spdy) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
2018-07-05 17:17:47 +02:00
| **Discovery** |
2019-03-15 15:18:27 +00:00
| [`interface-peer-discovery` ](//github.com/libp2p/interface-peer-discovery ) | [](//github.com/libp2p/interface-peer-discovery/releases) | [](https://david-dm.org/libp2p/interface-peer-discovery) | [](https://travis-ci.com/libp2p/interface-peer-discovery) | [](https://codecov.io/gh/libp2p/interface-peer-discovery) | N/A |
| [`libp2p-bootstrap` ](//github.com/libp2p/js-libp2p-bootstrap ) | [](//github.com/libp2p/js-libp2p-bootstrap/releases) | [](https://david-dm.org/libp2p/js-libp2p-bootstrap) | [](https://travis-ci.com/libp2p/js-libp2p-bootstrap) | [](https://codecov.io/gh/libp2p/js-libp2p-bootstrap) | [Vasco Santos ](mailto:vasco.santos@moxy.studio ) |
| [`libp2p-kad-dht` ](//github.com/libp2p/js-libp2p-kad-dht ) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos ](mailto:vasco.santos@moxy.studio ) |
| [`libp2p-mdns` ](//github.com/libp2p/js-libp2p-mdns ) | [](//github.com/libp2p/js-libp2p-mdns/releases) | [](https://david-dm.org/libp2p/js-libp2p-mdns) | [](https://travis-ci.com/libp2p/js-libp2p-mdns) | [](https://codecov.io/gh/libp2p/js-libp2p-mdns) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
| [`libp2p-rendezvous` ](//github.com/libp2p/js-libp2p-rendezvous ) | [](//github.com/libp2p/js-libp2p-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-rendezvous) | [](https://travis-ci.com/libp2p/js-libp2p-rendezvous) | [](https://codecov.io/gh/libp2p/js-libp2p-rendezvous) | N/A |
| [`libp2p-webrtc-star` ](//github.com/libp2p/js-libp2p-webrtc-star ) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos ](mailto:vasco.santos@moxy.studio ) |
| [`libp2p-websocket-star` ](//github.com/libp2p/js-libp2p-websocket-star ) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [](https://travis-ci.com/libp2p/js-libp2p-websocket-star) | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
2018-07-05 17:17:47 +02:00
| **Content Routing** |
2019-03-15 15:18:27 +00:00
| [`interface-content-routing` ](//github.com/libp2p/interface-content-routing ) | [](//github.com/libp2p/interface-content-routing/releases) | [](https://david-dm.org/libp2p/interface-content-routing) | [](https://travis-ci.com/libp2p/interface-content-routing) | [](https://codecov.io/gh/libp2p/interface-content-routing) | N/A |
| [`libp2p-delegated-content-routing` ](//github.com/libp2p/js-libp2p-delegated-content-routing ) | [](//github.com/libp2p/js-libp2p-delegated-content-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing) | [](https://travis-ci.com/libp2p/js-libp2p-delegated-content-routing) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
| [`libp2p-kad-dht` ](//github.com/libp2p/js-libp2p-kad-dht ) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos ](mailto:vasco.santos@moxy.studio ) |
2018-07-05 17:17:47 +02:00
| **Peer Routing** |
2019-03-15 15:18:27 +00:00
| [`interface-peer-routing` ](//github.com/libp2p/interface-peer-routing ) | [](//github.com/libp2p/interface-peer-routing/releases) | [](https://david-dm.org/libp2p/interface-peer-routing) | [](https://travis-ci.com/libp2p/interface-peer-routing) | [](https://codecov.io/gh/libp2p/interface-peer-routing) | N/A |
| [`libp2p-delegated-peer-routing` ](//github.com/libp2p/js-libp2p-delegated-peer-routing ) | [](//github.com/libp2p/js-libp2p-delegated-peer-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-peer-routing) | [](https://travis-ci.com/libp2p/js-libp2p-delegated-peer-routing) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing) | [Jacob Heun ](mailto:jacobheun@gmail.com ) |
| [`libp2p-kad-dht` ](//github.com/libp2p/js-libp2p-kad-dht ) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos ](mailto:vasco.santos@moxy.studio ) |
2019-09-03 10:29:03 +02:00
| **Utilities** |
2019-03-15 15:18:27 +00:00
| [`libp2p-crypto` ](//github.com/libp2p/js-libp2p-crypto ) | [](//github.com/libp2p/js-libp2p-crypto/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto) | [](https://travis-ci.com/libp2p/js-libp2p-crypto) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto) | [Friedel Ziegelmayer ](mailto:dignifiedquire@gmail.com ) |
| [`libp2p-crypto-secp256k1` ](//github.com/libp2p/js-libp2p-crypto-secp256k1 ) | [](//github.com/libp2p/js-libp2p-crypto-secp256k1/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto-secp256k1) | [](https://travis-ci.com/libp2p/js-libp2p-crypto-secp256k1) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto-secp256k1) | [Friedel Ziegelmayer ](mailto:dignifiedquire@gmail.com ) |
2019-09-03 10:29:03 +02:00
| **Data Types** |
| [`peer-book` ](//github.com/libp2p/js-peer-book ) | [](//github.com/libp2p/js-peer-book/releases) | [](https://david-dm.org/libp2p/js-peer-book) | [](https://travis-ci.com/libp2p/js-peer-book) | [](https://codecov.io/gh/libp2p/js-peer-book) | [Pedro Teixeira ](mailto:i@pgte.me ) |
| [`peer-id` ](//github.com/libp2p/js-peer-id ) | [](//github.com/libp2p/js-peer-id/releases) | [](https://david-dm.org/libp2p/js-peer-id) | [](https://travis-ci.com/libp2p/js-peer-id) | [](https://codecov.io/gh/libp2p/js-peer-id) | [Pedro Teixeira ](mailto:i@pgte.me ) |
| [`peer-info` ](//github.com/libp2p/js-peer-info ) | [](//github.com/libp2p/js-peer-info/releases) | [](https://david-dm.org/libp2p/js-peer-info) | [](https://travis-ci.com/libp2p/js-peer-info) | [](https://codecov.io/gh/libp2p/js-peer-info) | [Pedro Teixeira ](mailto:i@pgte.me ) |
2018-07-05 17:17:47 +02:00
| **Extensions** |
2019-03-15 15:18:27 +00:00
| [`libp2p-floodsub` ](//github.com/libp2p/js-libp2p-floodsub ) | [](//github.com/libp2p/js-libp2p-floodsub/releases) | [](https://david-dm.org/libp2p/js-libp2p-floodsub) | [](https://travis-ci.com/libp2p/js-libp2p-floodsub) | [](https://codecov.io/gh/libp2p/js-libp2p-floodsub) | [Vasco Santos ](mailto:vasco.santos@moxy.studio ) |
2016-11-25 20:07:45 +00:00
2016-09-02 15:22:28 -04:00
## Contribute
2017-01-16 19:01:09 +08:00
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:
2016-08-01 15:40:47 +02:00
2017-09-06 10:55:12 +02:00
- 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.
2016-08-01 15:40:47 +02:00
- **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.
2015-09-20 16:18:34 +01:00
2016-09-02 15:22:28 -04:00
## License
2015-09-20 16:18:34 +01:00
2018-10-19 17:37:34 +02:00
[MIT ](LICENSE ) © Protocol Labs