stream-channels
v1.4.1
Published
Simple one-way stream multiplexer with very few features
Downloads
30
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
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