1.3.2 • Published 6 years ago

cjs-wamp v1.3.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

WAMP Implementation

build status npm version dependencies status devDependencies status Gitter RunKit

WAMP lightweight implementation for both browser and server-side (with ws npm package).

cjs-wamp extends Emitter interface. It does not create any WebSocket connections but uses an existing one.

Installation

npm install cjs-wamp

Usage

Add the constructor to the scope:

var Wamp = require('cjs-wamp');

Create an instance from some existing WebSocket connection:

var ws   = new WebSocket('ws://echo.websocket.org'),
    wamp = new Wamp(ws);

Send message to execute remotely:

wamp.call('getInfo', {id: 128}, function ( error, result ) {
    // handle execution result
});

Serve remote request:

wamp.addListener('getData', function ( params, callback ) {
    // handle request ...
    // send back results to the sender
    callback(null, requestedData);
});

Send notification with some optional data:

wamp.call('onUserUpdate', newUserData);

Serve received notification:

wamp.addListener('onUserUpdate', function ( event ) {
    // handle notification data ...
});

Original WebSocket connection is also available:

wamp.socket.send('some message');

Catch the moment when WebSocket connection is ready:

wamp.socket.onopen = function() {
    // send or receive messages here
};

Server-side example with ws npm package:

var server = new (require('ws').Server)({port: 9000}),
    Wamp   = require('cjs-wamp');

server.on('connection', function ( connection ) {
    var wamp = new Wamp(connection);

    wamp.call('getInfo', {id: 128}, function ( error, result ) {
        // handle execution result
    });
});

Error codes

ValueMessageDescription
-32700Parse errorInvalid JSON data was received.
-32600Invalid RequestThe JSON sent is not a valid Request object.
-32601Method not foundThe method does not exist / is not available.

Contribution

If you have any problems or suggestions please open an issue according to the contribution rules.

License

cjs-wamp is released under the MIT License.