How to do end-to-end testing of Websocket Multiplexing in NodeJS

WebSocket-multiplex is a small library on top of SockJS that allows you to do multiplexing over a single SockJS connection. Websocket multiplexing is a great way to exchange data between different server-client modules using a single websocket connection. You receive a logical separation of functionality, while at same time don’t have to unnecessarily open new TCP connection for different modules.

I have used Websocket multiplexing extensively, but unit testing these modules has always been a concern.  You don’t always want to run browser client to do end-to-end testing for all the multiplexed channels.   To overcome this, I wrote a small nodejs module websocket-multiplex-client over the client component of websocket multiplex library.  This has given me great flexibility to do complete end-to-end testing of my server side code.

Example end-to-end Unit Test involving Websocket Multiplex

We start by creating a SockJS service and a Multiplex Server.

var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js"};
var service = sockjs.createServer(sockjs_opts);
var multiplexer = new websocket_mutliplex.MultiplexServer(service);

This Multiplex server will be hosted by an express app.  Register SockJS to except websocket connections on url multiplex for the app server.  For simplicity we will run the webserver on port 8088.

 var app = express();
 var server = http.createServer(app);
 service.installHandlers(server, {prefix:'/multiplex'});
 server.listen(8088);

Next step is to register channel names and their handlers on server side. For example, we will register two handlers for channels Channel1 and Channel2. These handlers will be invoked whenever Client sends any data on these channels.

var channel1 = multiplexer.registerChannel('channel1');
 
 channel1.on('connection', function(conn) {
            conn.on('data', function(data) {
                conn.write('Data received on channel1: ' + data);
            });
 }); 
          

 var channel2 = multiplexer.registerChannel('channel2');
 
 channel2.on('connection', function(conn) {
            conn.on('data', function(data) {
                conn.write('Data received on channel2: ' + data);
            });
 });

To emulate the client side, we will use sockjs-client-node module. This module allows us to emulate sockJS client connection in NodeJS.  Let’s start by opening a websocket connection to our app server.    As soon as the websocket connection is opened, we will use websocket-multiplex-client to create a client multiplex handler.

var sockjsClient = new sockjs_client("http://127.0.0.1:8088/multiplex", null, { rtt: 201 });
sockjsClient.onopen = function() {
 console.log("sockjsClient:Open");
 var client = new multiplex_client(sockjsClient);

Next step is to register our client with channel1 and channel2.  onmessage handler for a channel will be invoked when ever server sends any message on that channel.  Similarly,  you can send any message to the server channel module, by channel1.send or channel2.send.

var channel1_client = client.channel('channel1');
 channel1_client.onmessage = function(msg){
     console.log("Received message on channel 1", msg)
 };

 var channel2_client = client.channel('channel2');
 channel2_client.onmessage = function(msg){
     console.log("Received message on channel 2", msg)
 };
 
setTimeout(function() {
   channel1_client.send('Hi to Channel 1'); 
   channel2_client.send('Hello to Channel 2 '); 
  }, 100);

Npm Module Install

You can use the websocket-multiplex-client in your nodejs app by using the following command.

npm -g install websocket-multiplex-client

Code and More documentation.

You can find more details about the module at npm link https://www.npmjs.org/package/websocket-multiplex-client.  Code and Examples can be downloaded from github at https://github.com/sumitchawla/websocket-multiplex-client

Advertisements

7 thoughts on “How to do end-to-end testing of Websocket Multiplexing in NodeJS

  1. Hey, I just hopped over to your site by way of StumbleUpon. Now not one thing I would normally read, however I appreciated your thoughts none the less. Thanks for making something worth reading.

    Like

  2. I am planning on being a creative writing teacher for my Senior project. The problem is, I can’t think of a grade that kids start creative writing. I would rather deal with younger kids than older ones because in the end I want to give them a book of the classes collected works. I was thinking 3rd Grade, but I can’t remember when I started creative writing. Any and all help would be much appreciated. Thank you. :).

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s