js-libp2p/README.md

112 lines
3.5 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
2016-03-10 14:38:22 +00:00
## Install
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
2016-03-10 14:38:22 +00:00
## API
#### Create a libp2p Swarm
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.
2016-03-10 14:38:22 +00:00
### Transports
2016-03-10 14:38:22 +00:00
##### `swarm.transport.add(key, transport, options, callback)`
2016-03-10 14:38:22 +00:00
libp2p-swarm expects transports that implement [interface-transport](https://github.com/diasdavid/abstract-transport). For example [libp2p-tcp](https://github.com/diasdavid/js-libp2p-tcp).
2015-07-21 16:00:59 -07:00
2016-03-10 14:38:22 +00:00
- `key` - the transport identifier
- `transport` -
- `options`
- `callback`
2015-07-25 18:42:36 -07:00
2016-03-10 14:38:22 +00:00
##### `swarm.transport.dial(key, multiaddrs, callback)`
2015-07-25 18:42:36 -07:00
2016-03-10 14:38:22 +00:00
Dial to a peer on a specific transport.
2015-07-25 18:42:36 -07:00
2016-03-10 14:38:22 +00:00
- `key`
- `multiaddrs`
- `callback`
2015-07-25 18:42:36 -07:00
2016-03-10 14:38:22 +00:00
##### `swarm.transport.listen(key, options, handler, callback)`
2015-07-21 16:00:59 -07:00
2016-03-10 14:38:22 +00:00
Set a transport to start listening mode.
2015-07-21 16:00:59 -07:00
2016-03-10 14:38:22 +00:00
- `key`
- `options`
- `handler`
- `callback`
2015-07-21 16:00:59 -07:00
2016-03-10 14:38:22 +00:00
##### `swarm.transport.close(key, callback)`
2015-07-10 12:28:40 -07:00
2016-03-10 14:38:22 +00:00
Close the listeners of a given transport.
2015-07-21 16:00:59 -07:00
2016-03-10 14:38:22 +00:00
- `key`
- `callback`
2015-07-21 16:00:59 -07:00
2016-03-10 14:38:22 +00:00
### Connection
2015-07-21 16:00:59 -07:00
2016-03-10 14:38:22 +00:00
##### `swarm.connection.addUpgrade()`
2015-07-10 12:28:40 -07:00
2016-03-10 14:38:22 +00:00
A connection upgrade must be able to receive and return something that implements the [interface-connection](https://github.com/diasdavid/interface-connection) specification.
2016-03-10 14:38:22 +00:00
> **WIP**
2016-03-10 14:38:22 +00:00
##### `swarm.connection.addStreamMuxer(muxer)`
2016-03-10 14:38:22 +00: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 [interface-stream-muxer](https://github.com/diasdavid/interface-stream-muxer) spec.
2016-03-10 14:38:22 +00:00
- `muxer`
2016-03-10 14:38:22 +00:00
##### `swarm.connection.reuse()`
2016-03-10 14:38:22 +00:00
Enable the identify protocol
2016-03-10 14:38:22 +00:00
### `swarm.dial(pi, protocol, callback)`
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.
- `pi` - peer info project
- `protocol`
- `callback`
### `swarm.handle(protocol, handler)`
handle a new protocol.
- `protocol`
- `handler` - function called when we receive a dial on `protocol. Signature must be `function (conn) {}`
### `swarm.close(callback)`
close all the listeners and muxers.
- `callback`