sc
stream-channels
npm i stream-channels
sc

stream-channels

Simple one-way stream multiplexer with very few features

by Mathias Buus

1.4.1 (see all)License:MITTypeScript:Not Found
npm i stream-channels
Readme

stream-channels

Simple one-way stream multiplexer with very few features. For two-way (full duplex) multiplexing see the multiplex module.

npm install stream-channels

build status

Usage

var channels = require('stream-channels')
var stream = channels()

stream.on('channel', function (channel) {
  console.log('new channel')
  channel.on('data', console.log)
  channel.on('end', function () {
    console.log('(no more data)')
  })
})

var ch1 = stream.createChannel()
var ch2 = stream.createChannel()

ch1.write('hello')
ch2.write('world')

ch1.end()
ch2.end()

stream.pipe(stream)

API

var stream = channels([options], [onchannel])

Create a new instance. Options include:

{
  limit: maxChannelsAllowedOpen // defaults to 1024
}

var writeableStream = stream.createChannel()

Create a new channel.

stream.on('channel', readableStream)

Emitted when a remote creates a new channel. Will emit the data the remote writes to it.

stream.setTimeout(ms, [ontimeout])

Emit a timeout when if the stream is inactive for ~ms milliseconds. Will start a heartbeat as well.

Wire Protocol

The wire protocol is as follows.

------------------------------
| length | channel-id | data |
------------------------------
  • Length is a varint containing the binary length of channel-id and data.
  • Channel id is a varint representing the current channel.
  • Data is the buffer you wrote to a channel

Channels are lazily opened., The first time you receive data on a channel id, that channel is opened. Receiving an empty data buffer indicates that the channel is closed.

Messages received with length 0 should be ignored and can be used as a keep alive signal.

Back pressure

Back pressure will trigger on all channels when the slowest channel starts to back pressure.

License

MIT

Downloads/wk

9

GitHub Stars

25

LAST COMMIT

6yrs ago

MAINTAINERS

1

CONTRIBUTORS

1

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
1.4.1
latest
6yrs ago
No alternatives found
No tutorials found
Add a tutorial

Rate & Review

100
No reviews found
Be the first to rate