GithubHelp home page GithubHelp logo

shawnb457 / websocket-multiplex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sockjs/websocket-multiplex

0.0 2.0 0.0 60 KB

A thin library for doing multiplexing on top of SockJS

Home Page: https://github.com/sockjs/websocket-multiplex

websocket-multiplex's Introduction

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:

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 consists 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 genrally willing to copperate and no party can express any kind of errors. All invalid messages should be ignored.

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

websocket-multiplex's People

Contributors

majek avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.