Skip to content
/ wamp Public

Web Application Messaging Protocol lightweight implementation for both browser and server-side.

License

Notifications You must be signed in to change notification settings

cjssdk/wamp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Value Message Description
-32700 Parse error Invalid JSON data was received.
-32600 Invalid Request The JSON sent is not a valid Request object.
-32601 Method not found The 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.

About

Web Application Messaging Protocol lightweight implementation for both browser and server-side.

Resources

License

Stars

Watchers

Forks

Packages

No packages published