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.
npm install cjs-wamp
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
});
});
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. |
If you have any problems or suggestions please open an issue according to the contribution rules.
cjs-wamp
is released under the MIT License.