Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mux-demux

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mux-demux

multiplex-demultiplex object streams across _any_ text stream.

  • 3.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.6K
decreased by-60.65%
Maintainers
1
Weekly downloads
 
Created
Source

MuxDemux

multiplex-demultiplex object streams across any text stream.

var MuxDemux = require('..')
var net = require('net')

var mdm1 = MuxDemux()

var mdm2 = 
  MuxDemux(function (stream) {
    stream.on('data', console.log.bind(console))
  })

net.createServer(function (con) {
  con.pipe(mdm2).pipe(con)
}).listen(8642, function () {
  var con = net.connect(8642)
  con.pipe(mdm1).pipe(con)
  var ds = mdm1.createWriteStream('times')

  setInterval(function () {
    ds.write(new Date().toString())
  }, 1e3)
})

#API

the API browser-stream


var MuxDemux = require('mux-demux')
var client = MuxDemux()
var server = MuxDemux()

client.pipe(server).pipe(client)

server.on('connection', function (stream) {
  // inspect stream.meta to decide what this stream is.
})

client.createWriteStream(meta)
client.createReadStream(meta)
client.createStream(meta)

there is actually no distinction between clients and servers. if both sides are listening on('connection',...) then both sides may call create{Write,Read,}Stream(meta) and initiate new streams.

MuxDemux(options, onConnection)

Creates a MuxDemux stream. Optionally pass in an options hash

{
    error: Boolean,
    wrapper: function (stream) {...}
}

If the error option is set to true then MuxDemux will emit errors on the streams on unexpected disconnects. othewise, it will just emit 'end' on those streams.

wrapper be used to change the serialization format used by mux-demux, by default, line seperated json is used. see examples below both mux-demux end points must use the same wrapper.

options is optional. MuxDemux(onConnection) is a shortcut for MuxDemux().on('connection', onConnection)

createReadStream (meta)

open a ReadableStream from the other side. returns a ReadableStream. the other side of connection will emit a writable stream that is connected to this stream.

createWriteStream (meta)

open a WritableStream to the other side. returns a WritableStream, the other side will emit a ReadableStream connected to this stream.

createStream (meta)

open a Stream to the other side which is both readable and writable. returns a Stream, the other side will emit a Stream connected to this stream.

note to self, references to a class (Stream) should be capitalized, and in backticks. references to an instance should be lowercase, and not in backticks unless refuring to a specific variable in a code example.

Wrapper Examples

A stream of plain old js objects.

new MuxDemux({wrapper: function (stream) { return stream } })

A stream of msgpack.

var es = require('event-stream')
var ms = require('msgpack-stream')

new MuxDemux({wrapper: function (stream) { 
  return es.pipeline(ms.createDecodeStream(), stream, ms.createEncodeStream()) 
}})

MuxDemuxStream#error

there is one addition to the stream interface. call stream.error(err) will send an error that will be emitted at the other side of the stream. this is useful for sending 404 like messages to clients, etc.

FAQs

Package last updated on 18 Aug 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc