JavaScript implementation of https://github.com/libp2p/mplex
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
ci
examples
src
test
.aegir.js
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
appveyor.yml
circle.yml
package.json

README.md

js-libp2p-mplex

Build Status Dependency Status js-standard-style

JavaScript implementation of https://github.com/libp2p/mplex

Lead Maintainer

Vasco Santos.

Usage

Let's define a listener.js, which starts a TCP server on port 9999 and waits for a connection. Once we get a connection, we wait for a stream. And finally, once we have the stream, we pull the data from that stream, and printing it to the console.

const mplex = require('libp2p-mplex')
const tcp = require('net')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')

const listener = tcp.createServer((socket) => {
  console.log('[listener] Got connection!')

  const muxer = mplex.listener(toPull(socket))

  muxer.on('stream', (stream) => {
    console.log('[listener] Got stream!')
    pull(
      stream,
      pull.drain((data) => {
        console.log('[listener] Received:')
        console.log(data.toString())
      })
    )
  })
})

listener.listen(9999, () => {
  console.log('[listener] listening on 9999')
})

Now, let's define dialer.js who will connect to our listener over a TCP socket. Once we have that, we'll put a message in the stream for our listener.

const mplex = require('libp2p-mplex')
const tcp = require('net')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')

const socket = tcp.connect(9999)

const muxer = mplex.dialer(toPull(socket))

console.log('[dialer] opening stream')
const stream = muxer.newStream((err) => {
  console.log('[dialer] opened stream')
  if (err) throw err
})

pull(
  pull.values(['hey, how is it going. I am the dialer']),
  stream
)

Now we can first run listener.js and then dialer.js to see the following output:

listener.js

$ node listener.js
[listener] listening on 9999
[listener] Got connection!
[listener] Got stream!
[listener] Received:
hey, how is it going. I am the dialer

dialer.js

$ node dialer.js
[dialer] opening stream
[dialer] opened stream

Install

> npm install libp2p-mplex

API

const mplex = require('libp2p-mplex')