ws-squared

A simple wrapper protocol to handle application layer protocol inside the browser through websockets.

npm install ws-squared
4 downloads in the last week
16 downloads in the last month

WebSocket²

WebSocket² is a wrapper for application level protocols. It is intended to wrap the application interface from any application protocol into a websocket message frame and handle the results.

BEWARE: This is a first implementation, everything can change in here.

How to use it

Simply do a npm install ws-squared and then use it like this

var app         = require('express')(),
    http        = require('http').createServer(app),
    wssquared   = require('ws-squared');

http.listen(8001, function () { ... });

wssquared.register('customer.js');
wssquared.init(http);

The wssquared.register method registers a new api that needs to be mapped. The interface specification is not set yet. This is mere a first try.

The Customer Interface

A Customer handles a specific API through the WebSocket Connection. For example if you want to have access to a Modbus TCP Connection all you need to do is implement a modbus-customer.js with the following API:

exports.getName = function () {
    // returns the protocol name for example modbus
}

exports.getInstance = function () {
    // returns the instance of a specific socket
}

The Socket Interface

In order to map some Modules API over the WebSocket Connection you need to implement a Socket Module. A instance of a Socket Module will be passed to the ws-squared Agent as soon as it is requested from the browser side. The Socket Module must implement the following methods:

exports.setSender = function (sender) {
    // save the sender object for async messages to the client
}

exports.message = function (data, response) {
    // handles incoming data
    // respond by calling response(true, answer)
    // don't respond by calling response(false);
}

exports.close = function () {
    // close everything you've opened
}

The MIT License (MIT)

Copyright (c) 2013 Stefan Poeter stefan.poeter@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you