js-libp2p/README.md

99 lines
3.4 KiB
Markdown
Raw Normal View History

2015-10-29 00:26:18 +00:00
libp2p-swarm JavaScript implementation
======================================
2015-07-08 16:33:57 -07:00
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![Build Status](https://img.shields.io/travis/diasdavid/js-ipfs-swarm/master.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-ipfs-swarm)
2015-07-08 16:33:57 -07:00
2015-10-29 00:26:18 +00:00
> libp2p swarm implementation in JavaScript
2015-07-08 16:33:57 -07:00
# Description
2015-09-23 20:06:10 +01:00
libp2p-swarm is a connection abstraction that is able to leverage several transports and connection upgrades, such as congestion control, channel encryption, multiplexing several streams in one connection, and more. It does this by bringing protocol multiplexing to the application level (instead of the traditional Port level) using multicodec and multistream.
2015-07-09 15:45:03 -07:00
libp2p-swarm is used by libp2p but it can be also used as a standalone module.
2015-07-09 15:45:03 -07:00
2015-07-08 16:33:57 -07:00
# Usage
2015-07-09 15:45:03 -07:00
### Install and create a Swarm
2015-07-09 15:45:03 -07:00
libp2p-swarm is available on npm and so, like any other npm module, just:
2015-07-10 12:28:40 -07:00
```bash
> npm install libp2p-swarm --save
2015-07-21 16:00:59 -07:00
```
2015-07-10 12:28:40 -07:00
2015-09-23 20:06:10 +01:00
And use it in your Node.js code as:
2015-07-21 16:00:59 -07:00
```JavaScript
const Swarm = require('libp2p-swarm')
2015-07-21 16:00:59 -07:00
const sw = new Swarm(peerInfo)
2015-07-21 16:00:59 -07:00
```
peerInfo is a [PeerInfo](https://github.com/diasdavid/js-peer-info) object that represents the peer creating this swarm instance.
----------
BELOW NEEDS AN UPDATE
2015-07-21 16:00:59 -07:00
### Support a transport
2015-07-25 18:42:36 -07:00
2015-10-29 00:26:18 +00:00
libp2p-swarm expects transports that implement [abstract-transport](https://github.com/diasdavid/abstract-transport). For example [libp2p-tcp](https://github.com/diasdavid/js-libp2p-tcp), a simple shim on top of the `net` module to make it work with swarm expectations.
2015-07-25 18:42:36 -07:00
```JavaScript
sw.addTransport(transport, [options, dialOptions, listenOptions])
2015-07-25 18:42:36 -07:00
```
2015-09-23 20:06:10 +01:00
### Add a connection upgrade
2015-07-25 18:42:36 -07:00
2015-09-23 20:06:10 +01:00
A connection upgrade must be able to receive and return something that implements the [abstract-connection](https://github.com/diasdavid/abstract-connection) interface.
2015-07-21 16:00:59 -07:00
```JavaScript
sw.addUpgrade(connUpgrade, [options])
2015-07-21 16:00:59 -07:00
```
2015-09-23 20:06:10 +01:00
Upgrading a connection to use a stream muxer is still considered an upgrade, but a special case since once this connection is applied, the returned obj will implement the [abstract-stream-muxer](https://github.com/diasdavid/abstract-stream-muxer) interface.
2015-07-21 16:00:59 -07:00
```JavaScript
sw.addStreamMuxer(streamMuxer, [options])
2015-07-21 16:00:59 -07:00
```
2015-07-10 12:28:40 -07:00
### Dial to another peer
2015-07-21 16:00:59 -07:00
```JavaScript
sw.dial(PeerInfo, options, protocol, callback)
sw.dial(PeerInfo, options, callback)
2015-07-21 16:00:59 -07:00
```
2015-09-23 20:06:10 +01:00
dial uses the best transport (whatever works first, in the future we can have some criteria), and jump starts the connection until the point we have to negotiate the protocol. If a muxer is available, then drop the muxer onto that connection. Good to warm up connections or to check for connectivity. If we have already a muxer for that peerInfo, than do nothing.
2015-07-21 16:00:59 -07:00
### Accept requests on a specific protocol
2015-07-10 12:28:40 -07:00
```JavaScript
sw.handleProtocol(protocol, handlerFunction)
```
### Cleaning up before exiting
Each time you add a transport or dial you create connections. Be sure to close
them up before exiting. To do so you can:
Close a transport listener:
```js
sw.closeListener(transportName, callback)
sw.closeAllListeners(callback)
```
Close all open connections
```js
sw.closeConns(callback)
```
Close everything!
```js
sw.close(callback)
```