sockjs-multiplexer

Multiple virtual channels over a web socket / SockJS connection

Usage no npm install needed!

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

README

SockJS Multiplexer

Multiplexer for SockJS connections. Client and server agnostic.

Based on https://github.com/manuelstofer/websocket-multiplexer but optimised for SockJS.

Installation

npm install sockjs-multiplexer

API

Named channels:

var options = {
  // Serialization function for message
  // serialize: function () {}, // By default JSON.stringify
  //  Unserialization function for message
  // unserialize: function () {} // By default JSON.parse
};

var multiplexer = new Multiplexer(websocket, options),
    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);
    });
});

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;