libp2p
0.25.6

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

Libp2p

Extends EventEmitter.

Parameters

  1. _options: any:  

instance

Libp2p.prototype.emit

emit(eventName: string, args: ...any): void

Overrides EventEmitter.emit to conditionally emit errors if there is a handler. If not, errors will be logged.

Parameters

  1. eventName: string:  
  2. args: ...any:  

Returns

void

Libp2p.prototype.start

start(callback: function (Error)): void

Starts the libp2p node and all sub services

Parameters

  1. callback: function (Error) (=()=>{}):  

Returns

void

Libp2p.prototype.stop

stop(callback: function (Error)): void

Stop the libp2p node by closing its listeners and open connections

Parameters

  1. callback: function (Error) (=()=>{}):  

Returns

void

Libp2p.prototype.dial

dial(peer: (PeerInfo | PeerId | Multiaddr | string), callback: function (Error)): void

Dials to the provided peer. If successful, the PeerInfo of the peer will be added to the nodes PeerBook

Parameters

  1. peer: (PeerInfo | PeerId | Multiaddr | string):  
    The peer to dial
  2. callback: function (Error):  

Returns

void

Libp2p.prototype.dialProtocol

dialProtocol(peer: (PeerInfo | PeerId | Multiaddr | string), protocol: string, callback: function (Error, Connection)): void

Dials to the provided peer and handshakes with the given protocol. If successful, the PeerInfo of the peer will be added to the nodes PeerBook, and the Connection will be sent in the callback

Parameters

  1. peer: (PeerInfo | PeerId | Multiaddr | string):  
    The peer to dial
  2. protocol: string:  
  3. callback: function (Error, Connection):  

Returns

void

Libp2p.prototype.dialFSM

dialFSM(peer: (PeerInfo | PeerId | Multiaddr | string), protocol: string, callback: function (Error, ConnectionFSM)): void

Similar to dial and dialProtocol, but the callback will contain a Connection State Machine.

Parameters

  1. peer: (PeerInfo | PeerId | Multiaddr | string):  
    The peer to dial
  2. protocol: string:  
  3. callback: function (Error, ConnectionFSM):  

Returns

void

createLibp2p

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

Parameters

  1. options: object:  
    Libp2p configuration options
  2. callback: function (Error, Libp2p):  

Returns

void

findPeer

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

Parameters

  1. id: String:  
    The id of the peer to find
  2. options: object:  
  3. options.maxTimeout: number:  
    How long the query should run
  4. callback: function (Error, Result<Array>):  

Returns

void

findProviders

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

Parameters

  1. key: CID:  
    The CID key of the content to find
  2. options: object:  
  3. options.maxTimeout: number:  
    How long the query should run
  4. options.maxNumProviders: number:  
    maximum number of providers to find
  5. callback: function (Error, Result<Array>):  

Returns

void

provide

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

Parameters

  1. key: CID:  
    The CID key of the content to find
  2. callback: function (Error):  

Returns

void