Intro

Installable via npm install --save libp2p, it can also be used directly in the browser.

Download

The source is available for download from GitHub. Alternatively, you can install using npm:

$ npm install --save libp2p

You can then require() libp2p as normal:

const libp2P = require('libp2p')

In the Browser

Libp2p should work in any ES2015 environment out of the box.

Usage:

<script type="text/javascript" src="index.js"></script>

The portable versions of libp2p, including index.js and index.min.js, are included in the /dist folder. Libp2p can also be found on unpkg.com under

new Libp2p(_options: any)

Extends EventEmitter

Parameters
_options (any)
Instance Members
emit(eventName, args)
start(callback)
stop(callback)
dial(peer, callback)
dialProtocol(peer, protocol, callback)
dialFSM(peer, protocol, callback)
hangUp(peer, callback)
ping(peer, callback)

createLibp2p

src/index.js

Like new Libp2p(options) except it will create a PeerInfo instance if one is not provided in options.

createLibp2p
Parameters
options (object) Libp2p configuration options
callback (function (Error, Libp2p))
Returns
void:

Iterates over all peer routers in series to find the given peer.

findPeer
Parameters
id (String) The id of the peer to find
options (object)
Name Description
options.maxTimeout number How long the query should run
callback (function (Error, Result<Array>))
Returns
void:

Iterates over all content routers in series to find providers of the given key. Once a content router succeeds, iteration will stop.

findProviders
Parameters
key (CID) The CID key of the content to find
options (object)
Name Description
options.maxTimeout number How long the query should run
options.maxNumProviders number maximum number of providers to find
callback (function (Error, Result<Array>))
Returns
void:

Iterates over all content routers in parallel to notify it is a provider of the given key.

provide
Parameters
key (CID) The CID key of the content to find
callback (function (Error))
Returns
void:

subscribe

src/pubsub.js

Subscribe the given handler to a pubsub topic

subscribe
Parameters
topic (string)
handler (function) The handler to subscribe
options ((object | null)?)
callback (function?) An optional callback
Returns
(Promise | void): A promise is returned if no callback is provided
Example

Subscribe a handler to a topic

// `null` must be passed for options until subscribe is no longer using promisify
const handler = (message) => { }
await libp2p.subscribe(topic, handler, null)

Use a callback instead of the Promise api

// `options` may be passed or omitted when supplying a callback
const handler = (message) => { }
libp2p.subscribe(topic, handler, callback)

unsubscribe

src/pubsub.js

Unsubscribes from a pubsub topic

unsubscribe
Parameters
topic (string)
handler ((function | null)) The handler to unsubscribe from
callback (function?) An optional callback
Returns
(Promise | void): A promise is returned if no callback is provided
Example

Unsubscribe a topic for all handlers

// `null` must be passed until unsubscribe is no longer using promisify
await libp2p.unsubscribe(topic, null)

Unsubscribe a topic for 1 handler

await libp2p.unsubscribe(topic, handler)

Use a callback instead of the Promise api

libp2p.unsubscribe(topic, handler, callback)