npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sockjs-multiplex

v0.1.0

Published

SockJS-multiplex is a thin library on top of SockJS that allows you to do multiplexing of many virtual WebSockets connection over a single physical one.

Downloads

3

Readme

WebSocket-multiplex

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:

  • https://www.rabbitmq.com/blog/2012/02/23/how-to-compose-apps-using-websockets/

Usage from the browser

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

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

Alternatively, if you're using SSL:

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

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.

Protocol

The underlying protocol is quite simple. Each message is a string consisting of four comma separated parts: type, topic, id 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.

The topic identifies a channel registered on the server side.

The id a unique connection identifier generated on the client side. Each request to subscribe to a topic from a given client has a unique id. This makes it possible for a single client to open multiple independent channel connection to a single server-side service.

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.