A simple multiplex protocol for WebSocket
npm install @yume-chan/multiplex-websocket

A simple multiplex protocol for WebSocket
- Multiplex WebSocket
- Install
- Node.js
- Browsers
- API
- Development
- Install dependencies
- Testing
- Coverage
- License
`` shell`
npm install @yume-chan/multiplex-websocket
ws is a peer dependency, you need to install it separately for Node.js.
` shell`
npm install ws
This package uses Node.js built-in modules (Buffer, event and stream), so it needs polyfills to run in browsers.
Webpack should work, although not tested.
` ts
export default class MultiplexWebSocket {
static connect(url: string): Promise
constructor(raw: WebSocket);
addChannel(): MultiplexWebSocketChannel;
close(): void;
on(type: 'channel', listener: (channel: MultiplexWebSocketChannel, head: Buffer) => void): this;
on(type: 'error', listener: (error: Error) => void): this;
on(type: 'close', listener: () => void): this;
off(type: 'channel', listener: (channel: MultiplexWebSocketChannel, head: Buffer) => void): this;
off(type: 'error', listener: (error: Error) => void): this;
off(type: 'close', listener: () => void): this;
}
export class MultiplexWebSocketChannel extends Duplex { }
`
MultiplexWebSocketChannel is a duplex stream so you can use common stream operations (.read(), .write(), .pipe(), .on('data')) on it.
This project uses pnpm (GitHub) to manage dependency packages.
` shell`
pnpm i
You may also use npm, but the lockfile may become out of sync.
` shell`
npm test
` shell``
npm run coverage
MIT