# multiplex

> A binary stream multiplexer. Stream multiple streams of binary data over a single binary stream.

Latest version **6.7.0** (published 2016-03-07) · BSD 2-Clause license · 0 weekly downloads

## Install

```sh
npm install multiplex
pnpm add multiplex
yarn add multiplex
bun add multiplex
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.7.0 |
| Published | 2016-03-07 |
| First published | 2013-12-19 |
| Weekly downloads | 0 |
| License | BSD 2-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 143 |
| Author | max ogden |
| Maintainers | maxogden, substack, mafintosh, daviddias |

## Links

- npm: https://www.npmjs.com/package/multiplex
- Repository: https://github.com/maxogden/multiplex
- Issues: https://github.com/maxogden/multiplex/issues
- npm.io page: https://npm.io/package/multiplex

## Dependencies (5)

- [xtend](https://npm.io/package/xtend.md) ^4.0.0
- [varint](https://npm.io/package/varint.md) ^4.0.0
- [inherits](https://npm.io/package/inherits.md) ^2.0.1
- [duplexify](https://npm.io/package/duplexify.md) ^3.4.2
- [readable-stream](https://npm.io/package/readable-stream.md) ^2.0.2

## Recent versions

- 6.7.0 (latest) — 2016-03-07
- 6.6.1 — 2015-11-22
- 6.6.0 — 2015-11-14
- 6.5.0 — 2015-11-14
- 6.4.1 — 2015-10-24
- 6.4.0 — 2015-10-22
- 6.3.0 — 2015-10-21
- 6.2.2 — 2015-06-15
- 6.2.1 — 2015-05-28
- 6.2.0 — 2015-05-27
- 6.1.0 — 2015-05-27
- 6.0.5 — 2015-05-22
- 6.0.4 — 2015-05-18
- 6.0.3 — 2015-05-18
- 6.0.2 — 2015-05-18
- … 16 more at https://npm.io/package/multiplex/versions

## README

# multiplex

A binary stream multiplexer. Stream multiple streams of binary data over a single binary stream. Like [mux-demux](https://npmjs.org/package/mux-demux) but faster since it only works with binary streams.

[![NPM](https://nodei.co/npm/multiplex.png)](https://nodei.co/npm/multiplex/)

## api

### `var multiplex = require('multiplex')([options], [onStream])`

Returns a new multiplexer. You can use this to create sub-streams. All data written to sub-streams will be emitted through this. If you pipe a multiplex instance to another multiplex instance all substream data will be multiplexed and demultiplexed on the other end.

`onStream` will be called with `(stream, id)` whenever a new remote sub-stream is created with an id that hasn't already been created with `.createStream`.

Options include:

* `opts.limit` - set the max allowed message size. default is no maximum

Any other options set in `options` are used as defaults options when creating sub streams.

### `stream = multiplex.createStream([id], [options])`

Creates a new sub-stream with an optional whole string `id` (default is the stream channel id).

Sub-streams are duplex streams.

Options include:

* `opts.chunked` - enables chunked mode on all streams (message framing not guaranteed)
* `opts.halfOpen` - make channels support half open mode meaning that they can be readable but not writable and vice versa

### `stream = multiplex.receiveStream(id, [options])`

Explicitly receive an incoming stream.

This is useful if you have a function that accepts an instance of multiplex
and you want to receive a substream.

### `stream = multiplex.createSharedStream(id, [options])`

Create a shared stream. If both ends create a shared stream with
the same id, writing data on one end will emit the same data on the other end

## events

### `multiplex.on('error', function (err) {})`

Emitted when the outer stream encounters invalid data

### `multiplex.on('stream', function (stream, id) {})`

Emitted when a it a new stream arrives.

### `stream.on('error', function (err) {})`

Emitted if the inner stream is destroyed with an error

### example

```js
var multiplex = require('multiplex')
var plex1 = multiplex()
var stream1 = plex1.createStream()
var stream2 = plex1.createStream()

var plex2 = multiplex(function onStream(stream, id) {
  stream.on('data', function(c) {
    console.log('data', id, c.toString())
  })
})

plex1.pipe(plex2)

stream1.write(new Buffer('stream one!'))
stream2.write(new Buffer('stream two!'))
```

### contributing

multiplex is an **OPEN Open Source Project**. This means that:

> Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.

See the [CONTRIBUTING.md](contributing.md) file for more details.

### contributors

multiplex is only possible due to the excellent work of the following contributors:

<table><tbody><tr><th align="left">maxogden</th><td><a href="https://github.com/maxogden">GitHub/maxogden</a></td></tr>
<tr><th align="left">1N50MN14</th><td><a href="https://github.com/1N50MN14">GitHub/1N50MN14</a></td></tr>
<tr><th align="left">substack</th><td><a href="https://github.com/substack">GitHub/substack</a></td></tr>
<tr><th align="left">mafintosh</th><td><a href="https://github.com/mafintosh">GitHub/mafintosh</a></td></tr>
</tbody></table>

---
_Source: https://npm.io/package/multiplex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
