A thin library for doing multiplexing on top of SockJS
JavaScript Makefile
Switch branches/tags
Nothing to show
Clone or download
majek Merge pull request #18 from davidbz/patch-1
Adding MIT license file
Latest commit 9e9704a May 15, 2017
Failed to load latest commit information.
examples Tweak to server.js to fix #6 Jan 24, 2014
.gitignore Initial import Mar 16, 2012
LICENSE Adding MIT license file May 15, 2017
Makefile Initial import Mar 16, 2012
README.md spelling and flow Mar 5, 2014
VERSION-GEN Initial import Mar 16, 2012
multiplex_client.js Initial import Mar 16, 2012
multiplex_server.js Initial import Mar 16, 2012
package.json Initial import Mar 16, 2012



WebSocket-multiplex is a small library on top of SockJS that allows you to do multiplexing over a single SockJS connection.

The rationale for that is explained in details in the following blog post:

Usage from the browser

On the client side (browser) load library like that:

<script src="http://cdn.sockjs.org/websocket-multiplex-0.1.js">

Alternatively, if you're using SSL:

<script src="https://d1fxtkz8shb9d2.cloudfront.net/websocket-multiplex-0.1.js">

Usage example:

    var sockjs_url = '/multiplex';
    var sockjs = new SockJS(sockjs_url);

    var multiplexer = new WebSocketMultiplex(sockjs);
    var ann  = multiplexer.channel('ann');
    var bob  = multiplexer.channel('bob');
    var carl = multiplexer.channel('carl');

Usage from the node.js server

On the node.js server side, you can use npm to get the code:

npm install websocket-multiplex

And a simplistic example:

    var multiplex_server = require('websocket-multiplex');

    // 1. Setup SockJS server
    var service = sockjs.createServer();

    // 2. Setup multiplexing
    var multiplexer = new multiplex_server.MultiplexServer(service);

    var ann = multiplexer.registerChannel('ann');
    ann.on('connection', function(conn) {
        conn.write('Ann says hi!');
        conn.on('data', function(data) {
            conn.write('Ann nods: ' + data);

    // 3. Setup http server
    var server = http.createServer();
    sockjs_echo.installHandlers(server, {prefix:'/multiplex'});
    var app = express.createServer();

For a full-featured example see the /examples/sockjs directory.


The underlying protocol is quite simple. Each message is a string consisting of three comma separated parts: type, topic and payload. There are three valid message types:

  • sub - expresses a will to subscribe to a given topic.
  • msg - a message with payload is being sent on a topic.
  • uns - a will to unsubscribe from a topic.

Invalid messages like wrong unsubscriptions or publishes to a topic to which a client was not subscribed to are simply ignored.

This protocol assumes that both parties are generally willing to cooperate and that no party makes errors. All invalid messages should be ignored.

It's important to notice that the namespace is shared between both parties. It is not a good idea to use the same topic names on the client and on the server side because both parties may unsubscribe the other from a topic.