Protomux
Multiplex multiple message-oriented protocols over a stream
Installation
Install with npm:
Basic usage
API
mux = new Protomux(stream, [options])
mux = new Protomux(stream, [options])
Makes a new instance. stream
should be a framed stream, preserving the messages written.
options
include:
mux = Protomux.from(stream | muxer, [options])
mux = Protomux.from(stream | muxer, [options])
Helper to accept either an existing muxer instance or a stream (which creates a new one).
const channel = mux.createChannel([options])
Adds a new protocol channel.
options
include:
Sessions are paired based on a queue, so the first remote channel with the same protocol
and id
.
mux.createChannel
returnsnull
if the channel should not be opened, it's a duplicate channel or the remote has already closed this one. To have multiple sessions with the sameprotocol
andid
, setunique: false
as an option.
const opened = mux.opened({ protocol, id })
const opened = mux.opened({ protocol, id })
Boolean that indicates if the channel is opened.
mux.pair({ protocol, id }, callback)
mux.pair({ protocol, id }, callback)
Registers a callback to be called every time a new channel is requested.
mux.unpair({ protocol, id })
mux.unpair({ protocol, id })
Unregisters the pair callback.
channel.open([handshake])
channel.open([handshake])
Opens the channel.
const m = channel.addMessage([options])
const m = channel.addMessage([options])
Adds/registers a message type for a specific encoding. Options include:
m.send(data)
m.send(data)
Sends a message.
m.onmessage
m.onmessage
The function that is called when a message arrives.
m.encoding
m.encoding
The encoding for this message.
channel.close()
channel.close()
Closes the protocol channel.
channel.cork()
channel.cork()
Corking the protocol channel, makes it buffer messages and sends them all in a batch when it uncorks.
channel.uncork()
channel.uncork()
Uncorks and send the batch.
mux.cork()
mux.cork()
Same as channel.cork
but on the muxer instance.
mux.uncork()
mux.uncork()
Same as channel.uncork
but on the muxer instance.
for (const channel of muxer) { ... }
for (const channel of muxer) { ... }
The muxer instance is iterable so all channels can be iterated.
Last updated