js-libp2p/README.md

70 lines
2.8 KiB
Markdown
Raw Normal View History

libp2p-swarm Node.js implementation
2015-07-08 16:33:57 -07:00
=================================
2015-07-31 18:02:38 +02: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/node-ipfs-swarm/master.svg?style=flat-square)](https://travis-ci.org/diasdavid/node-ipfs-swarm)
2015-07-08 16:33:57 -07:00
> libp2p swarm implementation in Node.js
2015-07-08 16:33:57 -07:00
# Description
libp2p-swarm is connection abstraction that is able to leverage several transports and connection upgrades (such as congestion control, encrypt a channel, multiplex 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
And use it on your Node.js code as:
2015-07-21 16:00:59 -07:00
```JavaScript
var Swarm = require('libp2p-swarm')
2015-07-21 16:00:59 -07:00
var sw = new Swarm(peerInfoSelf)
2015-07-21 16:00:59 -07:00
```
peerInfoSelf is a PeerInfo object that represents the peer creating this swarm instance.
2015-07-21 16:00:59 -07:00
### Support a transport
2015-07-25 18:42:36 -07:00
libp2p-swarm expects transports that implement [abstract-transport](https://github.com/diasdavid/abstract-transport). For example [libp2p-tcp](https://github.com/diasdavid/node-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
```
### Add an connection upgrade
2015-07-25 18:42:36 -07:00
A connection upgrade must be able to receive and return something that implements the [abstract-connection]() interface.
2015-07-21 16:00:59 -07:00
```JavaScript
sw.addUpgrade(connUpgrade, [options])
2015-07-21 16:00:59 -07:00
```
Upgrading a connection to use a Stream Muxer is still considered a upgrade, but a special case since once this connection is applied, the returned obj will implement the [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
2015-09-21 16:46:04 +01:00
sw.dial(PeerInfo, options, protocol)
sw.dial(PeerInfo, options)
2015-07-21 16:00:59 -07: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)
```