/js-multistream-select

JavaScript implementation of multistream-select

Primary LanguageJavaScriptMIT LicenseMIT

js-multistream-select

Coverage Status Travis CI Circle CI Dependency Status js-standard-style

JavaScript implementation of multistream-select.

Lead Maintainer

Jacob Heun

Table of Contents

Background

What is multistream-select

tl;dr: multistream-select is protocol multiplexing per connection/stream. Full spec here

Select a protocol flow

The caller will send "interactive" messages, expecting for some acknowledgement from the callee, which will "select" the handler for the desired and supported protocol

< /multistream-select/0.3.0  # i speak multistream-select/0.3.0
> /multistream-select/0.3.0  # ok, let's speak multistream-select/0.3.0
> /ipfs-dht/0.2.3            # i want to speak ipfs-dht/0.2.3
< na                         # ipfs-dht/0.2.3 is not available
> /ipfs-dht/0.1.9            # What about ipfs-dht/0.1.9 ?
< /ipfs-dht/0.1.9            # ok let's speak ipfs-dht/0.1.9 -- in a sense acts as an ACK
> <dht-message>
> <dht-message>
> <dht-message>

This mode also packs a ls option, so that the callee can list the protocols it currently supports

Install

npm

> npm i multistream-select

Setup

Node.js

const multistream = require('multistream-select')

Browser: Browserify, Webpack, other bundlers

The code published to npm that gets loaded on require is in fact a ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process.

const multistream = require('multistream-select')

Browser: <script> Tag

Loading this module through a script tag will make the MultistreamSelect obj available in the global namespace.

<script src="https://unpkg.com/multistream-select/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/multistream-select/dist/index.js"></script>

Usage

Attach multistream to a connection (socket)

const multistream = require('multistream-select')

const ms = new multistream.Listener()
// or
const ms = new multistream.Dialer()

// apply the multistream to the conn
ms.handle(conn, callback)

This module uses pull-streams

We expose a streaming interface based on pull-streams, rather than on the Node.js core streams implementation (aka Node.js streams). pull-streams offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this issue.

You can learn more about pull-streams at:

Converting pull-streams to Node.js Streams

If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module pull-stream-to-stream, giving you an instance of a Node.js stream that is linked to the pull-stream. For example:

const pullToStream = require('pull-stream-to-stream')

const nodeStreamInstance = pullToStream(pullStreamInstance)
// nodeStreamInstance is an instance of a Node.js Stream

To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.

API

https://multiformats.github.io/js-multistream-select/

Maintainers

Captain: @diasdavid.

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2015 David Dias