font-weight: bold;}.hljs-deletion{background: #fdd;}.hljs-addition{background: #dfd;}.hljs-emphasis{font-style: italic;}.hljs-strong{font-weight: bold;}</style><divstyle="margin-bottom:104px;"data-radium="true"><h1data-radium="true"><aclass="anchor"name="intro"></a>Intro</h1><div><p>Installable via <code>npm install --save libp2p</code>, it can also be used directly in the browser.</p>
<p>The source is available for download from <ahref="https://github.com/libp2p/js-libp2p.git">GitHub</a>. Alternatively, you can install using npm:</p>
<p>The portable versions of libp2p, including <code>index.js</code> and <code>index.min.js</code>, are included in the <code>/dist</code> folder. Libp2p can also be found on <ahref="https://unpkg.com">unpkg.com</a> under</p>
</span></div></div></div><divstyle="margin-bottom:52px;"data-radium="true"><h3data-radium="true"><aclass="anchor"name="libp2pstart"></a>Libp2p.prototype.start<ahref="https://github.com/libp2p/js-libp2p/blob/8e95df7f22e90f424f14bc6a2e61c6291484496c/src/index.js#L212-L215"title="src/index.js"style="float:right;"data-radium="true"><svgfill="currentColor"preserveAspectRatio="xMidYMid meet"height="1em"width="1em"viewBox="0 0 40 40"style="vertical-align:middle;"><g><pathd="m26.3 7.5l-3.8 3.8 8.8 8.7-8.8 8.8 3.8 3.7 11.2-12.5-11.2-12.5z m-12.5 0l-11.3 12.5 11.3 12.5 3.7-3.7-8.7-8.8 8.7-8.7-3.7-3.8z"></path></g></svg></a></h3><pre><code>start(callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>)): void</code></pre><divstyle="margin-top:13px;margin-bottom:13px;"data-radium="true"><p>Starts the libp2p node and all sub services</p>
</div><divdata-radium="true"><h4>Parameters</h4><olstyle="list-style:none;margin-left:0;padding-left:0;"data-radium="true"><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>) (=()=>{})</code>: <divstyle="display:inline-block;"data-radium="true"></div></li></div></ol></div><divdata-radium="true"><h4>Returns</h4><divdata-radium="true"><code>void</code><span>
</span></div></div></div><divstyle="margin-bottom:52px;"data-radium="true"><h3data-radium="true"><aclass="anchor"name="libp2pstop"></a>Libp2p.prototype.stop<ahref="https://github.com/libp2p/js-libp2p/blob/8e95df7f22e90f424f14bc6a2e61c6291484496c/src/index.js#L223-L226"title="src/index.js"style="float:right;"data-radium="true"><svgfill="currentColor"preserveAspectRatio="xMidYMid meet"height="1em"width="1em"viewBox="0 0 40 40"style="vertical-align:middle;"><g><pathd="m26.3 7.5l-3.8 3.8 8.8 8.7-8.8 8.8 3.8 3.7 11.2-12.5-11.2-12.5z m-12.5 0l-11.3 12.5 11.3 12.5 3.7-3.7-8.7-8.8 8.7-8.7-3.7-3.8z"></path></g></svg></a></h3><pre><code>stop(callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>)): void</code></pre><divstyle="margin-top:13px;margin-bottom:13px;"data-radium="true"><p>Stop the libp2p node by closing its listeners and open connections</p>
</div><divdata-radium="true"><h4>Parameters</h4><olstyle="list-style:none;margin-left:0;padding-left:0;"data-radium="true"><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>) (=()=>{})</code>: <divstyle="display:inline-block;"data-radium="true"></div></li></div></ol></div><divdata-radium="true"><h4>Returns</h4><divdata-radium="true"><code>void</code><span>
</span></div></div></div><divstyle="margin-bottom:52px;"data-radium="true"><h3data-radium="true"><aclass="anchor"name="libp2pdial"></a>Libp2p.prototype.dial<ahref="https://github.com/libp2p/js-libp2p/blob/8e95df7f22e90f424f14bc6a2e61c6291484496c/src/index.js#L240-L242"title="src/index.js"style="float:right;"data-radium="true"><svgfill="currentColor"preserveAspectRatio="xMidYMid meet"height="1em"width="1em"viewBox="0 0 40 40"style="vertical-align:middle;"><g><pathd="m26.3 7.5l-3.8 3.8 8.8 8.7-8.8 8.8 3.8 3.7 11.2-12.5-11.2-12.5z m-12.5 0l-11.3 12.5 11.3 12.5 3.7-3.7-8.7-8.8 8.7-8.7-3.7-3.8z"></path></g></svg></a></h3><pre><code>dial(peer: (PeerInfo | PeerId | Multiaddr | <ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>), callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>)): void</code></pre><divstyle="margin-top:13px;margin-bottom:13px;"data-radium="true"><p>Dials to the provided peer. If successful, the <code>PeerInfo</code> of the
</div></li></div><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>)</code>: <divstyle="display:inline-block;"data-radium="true"></div></li></div></ol></div><divdata-radium="true"><h4>Returns</h4><divdata-radium="true"><code>void</code><span>
</span></div></div></div><divstyle="margin-bottom:52px;"data-radium="true"><h3data-radium="true"><aclass="anchor"name="libp2pdialprotocol"></a>Libp2p.prototype.dialProtocol<ahref="https://github.com/libp2p/js-libp2p/blob/8e95df7f22e90f424f14bc6a2e61c6291484496c/src/index.js#L254-L269"title="src/index.js"style="float:right;"data-radium="true"><svgfill="currentColor"preserveAspectRatio="xMidYMid meet"height="1em"width="1em"viewBox="0 0 40 40"style="vertical-align:middle;"><g><pathd="m26.3 7.5l-3.8 3.8 8.8 8.7-8.8 8.8 3.8 3.7 11.2-12.5-11.2-12.5z m-12.5 0l-11.3 12.5 11.3 12.5 3.7-3.7-8.7-8.8 8.7-8.7-3.7-3.8z"></path></g></svg></a></h3><pre><code>dialProtocol(peer: (PeerInfo | PeerId | Multiaddr | <ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>), protocol: <ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>, Connection)): void</code></pre><divstyle="margin-top:13px;margin-bottom:13px;"data-radium="true"><p>Dials to the provided peer and handshakes with the given protocol.
</div></li></div><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>, <ahref="#libp2p">Libp2p</a>)</code>: <divstyle="display:inline-block;"data-radium="true"></div></li></div></ol></div><divdata-radium="true"><h4>Returns</h4><divdata-radium="true"><code>void</code><span>
</span></div></div></div><divstyle="margin-bottom:104px;"data-radium="true"><h1data-radium="true"><aclass="anchor"name="findpeer"></a>findPeer<ahref="https://github.com/libp2p/js-libp2p/blob/8e95df7f22e90f424f14bc6a2e61c6291484496c/src/peer-routing.js#L24-L56"title="src/peer-routing.js"style="float:right;"data-radium="true"><svgfill="currentColor"preserveAspectRatio="xMidYMid meet"height="1em"width="1em"viewBox="0 0 40 40"style="vertical-align:middle;"><g><pathd="m26.3 7.5l-3.8 3.8 8.8 8.7-8.8 8.8 3.8 3.7 11.2-12.5-11.2-12.5z m-12.5 0l-11.3 12.5 11.3 12.5 3.7-3.7-8.7-8.8 8.7-8.7-3.7-3.8z"></path></g></svg></a></h1><div><p>Iterates over all peer routers in series to find the given peer.</p>
</div><divdata-radium="true"><h4>Parameters</h4><olstyle="list-style:none;margin-left:0;padding-left:0;"data-radium="true"><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>id: <ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></code>: <divstyle="display:inline-block;"data-radium="true">The id of the peer to find
</div></li></div><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>options: <ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">object</a></code>: <divstyle="display:inline-block;"data-radium="true"></div></li><listyle="padding-bottom:13px;"data-radium="true"><code>options.maxTimeout: <ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></code>: <divstyle="display:inline-block;"data-radium="true">How long the query should run
</div></li></div><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>, Result<<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a>>)</code>: <divstyle="display:inline-block;"data-radium="true"></div></li></div></ol></div><divdata-radium="true"><h4>Returns</h4><divdata-radium="true"><code>void</code><span>
</span></div></div></div><divstyle="margin-bottom:104px;"data-radium="true"><h1data-radium="true"><aclass="anchor"name="findproviders"></a>findProviders<ahref="https://github.com/libp2p/js-libp2p/blob/8e95df7f22e90f424f14bc6a2e61c6291484496c/src/content-routing.js#L27-L63"title="src/content-routing.js"style="float:right;"data-radium="true"><svgfill="currentColor"preserveAspectRatio="xMidYMid meet"height="1em"width="1em"viewBox="0 0 40 40"style="vertical-align:middle;"><g><pathd="m26.3 7.5l-3.8 3.8 8.8 8.7-8.8 8.8 3.8 3.7 11.2-12.5-11.2-12.5z m-12.5 0l-11.3 12.5 11.3 12.5 3.7-3.7-8.7-8.8 8.7-8.7-3.7-3.8z"></path></g></svg></a></h1><div><p>Iterates over all content routers in series to find providers of the given key.
</div><divdata-radium="true"><h4>Parameters</h4><olstyle="list-style:none;margin-left:0;padding-left:0;"data-radium="true"><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>key: CID</code>: <divstyle="display:inline-block;"data-radium="true">The CID key of the content to find
</div></li></div><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>options: <ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">object</a></code>: <divstyle="display:inline-block;"data-radium="true"></div></li><listyle="padding-bottom:13px;"data-radium="true"><code>options.maxTimeout: <ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></code>: <divstyle="display:inline-block;"data-radium="true">How long the query should run
</div></li><listyle="padding-bottom:13px;"data-radium="true"><code>options.maxNumProviders: <ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></code>: <divstyle="display:inline-block;"data-radium="true">maximum number of providers to find
</div></li></div><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>, Result<<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a>>)</code>: <divstyle="display:inline-block;"data-radium="true"></div></li></div></ol></div><divdata-radium="true"><h4>Returns</h4><divdata-radium="true"><code>void</code><span>
</span></div></div></div><divstyle="margin-bottom:104px;"data-radium="true"><h1data-radium="true"><aclass="anchor"name="provide"></a>provide<ahref="https://github.com/libp2p/js-libp2p/blob/8e95df7f22e90f424f14bc6a2e61c6291484496c/src/content-routing.js#L73-L81"title="src/content-routing.js"style="float:right;"data-radium="true"><svgfill="currentColor"preserveAspectRatio="xMidYMid meet"height="1em"width="1em"viewBox="0 0 40 40"style="vertical-align:middle;"><g><pathd="m26.3 7.5l-3.8 3.8 8.8 8.7-8.8 8.8 3.8 3.7 11.2-12.5-11.2-12.5z m-12.5 0l-11.3 12.5 11.3 12.5 3.7-3.7-8.7-8.8 8.7-8.7-3.7-3.8z"></path></g></svg></a></h1><div><p>Iterates over all content routers in parallel to notify it is
</div><divdata-radium="true"><h4>Parameters</h4><olstyle="list-style:none;margin-left:0;padding-left:0;"data-radium="true"><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>key: CID</code>: <divstyle="display:inline-block;"data-radium="true">The CID key of the content to find
</div></li></div><divdata-radium="true"><listyle="padding-bottom:13px;"data-radium="true"><code>callback: function (<ahref="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>)</code>: <divstyle="display:inline-block;"data-radium="true"></div></li></div></ol></div><divdata-radium="true"><h4>Returns</h4><divdata-radium="true"><code>void</code><span>