websocket-multiplexer

Multiple virtual channels over a web socket / SockJS connection

Usage no npm install needed!

<script type="module">
  import websocketMultiplexer from 'https://cdn.skypack.dev/websocket-multiplexer';
</script>

README

Websocket multiplexer

Websocket multiplexer, emulates virtual channels over web socket or SockJS.

Its different to the SockJS multiplexer in some ways:

  • Client / Server agnostic.
  • Anonymous channels.
  • Open new channels at any time. No need to specify them ahead.
  • CommonJS, use it with component, browserify or as node.js module.

Installation

component install manuelstofer/websocket-multiplexer
npm install websocket-multiplexer
var Multiplexer = require('websocket-multiplexer');

API

With named channels:

var multiplexer = new Multiplexer({ socket: websocket }),
    example = multiplexer.channel('example');

example.addEventListener('message', function (evt) {
    console.log(evt.data);
});

example.addEventListener('close', function (evt) {

});

Create anonymous channels:

var multiplexer = new Multiplexer({ socket: websocket }),
    channel = multiplexer.channel();

channel.send('hello');

Listen for anonymous channels:

var multiplexer = new Multiplexer({ socket: websocket });

multiplexer.addEventListener('channel', function (evt) {
    var channel = evt.channel;

    channel.addEventListener('message', function (evt) {
        console.log(evt.data);
    });
});

There are also some examples for node.js + ws in examples/ws

Multiplexer

interface Multiplexer {
  attribute Function onchannel;

  // create a channel
  void channel(optional String channel_id);
  void close();
};
Multiplexer implements EventTarget;

Channel

interface Channel {
  attribute String name;
  attribute Function onmessage;
  attribute Function onclose;
  void send(in DOMString data);
  void close();
};
Channel implements EventTarget;