Feature-rich lightweight WAMP (Web Application Messaging Protocol) Javascript implementation
Key metrics and engagement data
Repository has been active for 11 years, 7 months
⭐50
Want deeper insights? Explore GitObs.com
Amazingly fast, feature-rich, lightweight and zero dependency (by default) WAMP (Web Application Messaging Protocol) Javascript client (for browser and node.js)
Wampy.js is javascript library, that runs both in browser and node.js environments, and even in react native environment. It implements WAMP v2 specification on top of WebSocket object, also provides additional features like auto-reconnecting. It has no external dependencies (by default) and is easy to use. Also, it provides (starting from v7.1) command line wamp client which can be extremely helpful in quick check/debug existing WAMP-based APIs.
Wampy.js supports the following WAMP roles and features:
Wampy supports the following serializers:
In node.js environment Wampy is compatible with the following websocket clients:
For convenience, all API documentation is also available as a GitBook here.
javascript1const wampy = new Wampy('/ws/', { realm: 'AppRealm' });23try {4 await wampy.connect();5} catch (e) {6 console.log('connection failed', e);7}89try {10 await wampy.subscribe('system.monitor.update', (eventData) => {11 console.log('Received event:', eventData);12 });13} catch (e) {14 console.log('subscription failed', e);15}1617try {18 const res = await wampy.call('get.server.time');19 console.log('RPC called. Server time:', res.argsDict.serverTime);20} catch (e) {21 console.log('RPC call failed', e);22}2324// Somewhere else for example25await wampy.publish('system.monitor.update');2627// or just ignore promise if you don't need it28wampy.publish('client.message', 'Hi guys!');
Wampy.js can be installed using npm:
shell1npm install -S wampy
For browser usage download the latest browser.zip archive and add wampy-all.min.js file to your page. It contains all auth plugins and serializers. They are available as global objects:
javascript1window.JsonSerializer = JsonSerializer;2window.MsgpackSerializer = MsgpackSerializer;3window.CborSerializer = CborSerializer;4// this is not available due to problem with browserify transforming await import('node:crypto')5// if someone really needs it - I would appreciate a PR with fix :)6//window.WampyCra = wampyCra;7window.WampyCryptosign = wampyCryptosign;
html1<script src="browser/wampy-all.min.js"></script>
If you don't plan to use other serializers then JSON or any auth plugins, just include wampy.min.js.
html1<script src="browser/wampy.min.js"></script>
Wampy.js exports next components that you can import at your needs:
json51{2 "exports": {3 ".": { // Main Wampy class4 "import": "./src/wampy.js",5 "require": "./dist/wampy.js"6 },7 "./JsonSerializer.js": {8 "import": "./src/serializers/json-serializer.js",9 "require": "./dist/serializers/json-serializer.js"10 },11 "./CborSerializer.js": {12 "import": "./src/serializers/cbor-serializer.js",13 "require": "./dist/serializers/cbor-serializer.js"14 },15 "./MsgpackSerializer.js": {16 "import": "./src/serializers/msgpack-serializer.js",17 "require": "./dist/serializers/msgpack-serializer.js"18 },19 "./cryptosign.js": { // Cryptosign authentication plugin20 "import": "./src/auth/cryptosign/wampy-cryptosign.js",21 "require": "./dist/auth/cryptosign/wampy-cryptosign.js"22 },23 "./wampcra.js": { // WAMP-CRA authentication plugin24 "import": "./src/auth/wampcra/wampy-cra.js",25 "require": "./dist/auth/wampcra/wampy-cra.js"26 }27 }28}
Wampy cli tool exposes almost the same API options to the command line interface. You can use all types of
authorization, publish, subscribe, register and call any URI. Some WAMP Actions provides additional helper options
(e.g. mirror
option in register command that allows to return back the invocation payload to the caller).
Cli tool is charged with rich help descriptions, examples and even shell auto-completion script. All parameters may be passed as cmd args or via related ENV Vars for convenience. So you can for example export WAMP Router URI and realm to the environment and provide only wamp action parameters via cmd.
You can install wampy cli tool globally or call it by using npx:
shell1npm install -g wampy2# After that you can invoke wampy3wampy -h4# or just run wampy with npx5npx wampy -h
Check the wampy -h
or wampy --help
for the available commands and global options and check the help for a specific
command by issuing wampy <call|register|publish|subscribe> -h
.
To make use of shell auto-completion features just add output of wampy completion
to your shell config:
shell1wampy completion >> ~/.zshrc2# or3wampy completion >> ~/.bashrc
The completion
command is hidden from the wampy -h
output to not pollute the main use flow as it is only needed
once.
Please refer to Migrating.md for instructions on upgrading major versions.
Below is a description of the exposed public API. Wampy also has type definitions available at DefinitelyTyped.org, but they are only for versions < 7.x for now. Feel free to update!)
Wampy constructor can take 2 parameters:
realm
. For node.js environment it's also necessary to specify ws
- websocket module. See description below.javascript1// in browser2wampy = new Wampy();3wampy = new Wampy('/my-socket-path');4wampy = new Wampy('wss://socket.server.com:5000/ws', { autoReconnect: false });5wampy = new Wampy({ reconnectInterval: 1*1000 });67// in node.js8import { w3cwebsocket as w3cws } from 'websocket';9wampy = new Wampy(null, { ws: w3cws });10wampy = new Wampy('/my-socket-path', { ws: w3cws });11wampy = new Wampy('wss://socket.server.com:5000/ws', { autoReconnect: false, ws: w3cws });12wampy = new Wampy({ reconnectInterval: 1*1000, ws: w3cws });1314// or using ws example15import WebSocket from 'ws';16wampy = new Wampy(null, { ws: WebSocket });17wampy = new Wampy('/my-socket-path', { ws: WebSocket });18wampy = new Wampy('wss://socket.server.com:5000/ws', { autoReconnect: false, ws: WebSocket });19wampy = new Wampy({ reconnectInterval: 1*1000, ws: WebSocket });
Json serializer will be used by default. If you want to use msgpack or cbor serializer, pass it through options. Also, you can use your own serializer if it is supported on the WAMP router side.
javascript1// in browser2wampy = new Wampy('wss://socket.server.com:5000/ws', {3 serializer: new MsgpackSerializer()4});5wampy = new Wampy({6 serializer: new CborSerializer()7});89// in node.js10import { Wampy } from 'wampy';11import { MsgpackSerializer } from 'wampy/MsgpackSerializer';12import { CborSerializer } from 'wampy/CborSerializer';13import WebSocket from 'ws';1415wampy = new Wampy('wss://socket.server.com:5000/ws', {16 ws: WebSocket,17 serializer: new MsgpackSerializer()18});19wampy = new Wampy({20 ws: w3cws,21 serializer: new CborSerializer()22});23
.options() method is now deprecated, so this is here only for documentation purposes. Please
use getOptions()/setOptions()
instead.
.options() can be called in two forms: -- without parameters it will behave the same as new method getOptions() -- with one parameter as a hash-table it will behave the same as new method setOptions()
javascript1wampy.options(); // same as wampy.getOptions23wampy.options({ // same as wampy.setOptions4 authPlugins: {5 ticket: ((userPassword) => (() => userPassword ))(),6 wampcra: wampyCra.sign(secret),7 cryptosign: wampyCryptosign.sign(privateKey)8 },9 authMode: 'auto'10});
Returns Wampy configuration options. See setOptions()
down below for the full list of available options.
javascript1wampy.getOptions();
Receives a newOptions object as a parameter, where each property is a new option to be set and returns a Wampy instance.
Options attributes description:
false
. Enable debug logging.null
. User-provided logging function. If debug=true
and no logger
specified, console.log
will be used.true
. Enable auto reconnecting. In case of connection failure, Wampy will try to reconnect to WAMP server, and if you were subscribed to any topics, or had registered some procedures, Wampy will resubscribe to that topics and reregister procedures.value
: 2000 (ms). Reconnection Interval in ms.25
. Max reconnection attempts. After reaching this value .disconnect()
will be called. Set to 0 to disable limit.null
. WAMP Realm to join on server. See WAMP spec for additional info.null
. Custom attributes to send to router on hello.strict
. Can be changed to loose
for less strict URI validation.null
. Authentication (user) id to use in challenge.[]
. Array of strings of supported authentication methods.{}
. Additional authentication options for Cryptosign-based authentication.
See Cryptosign-based Authentication section and WAMP Spec CS for more info.{}
. Authentication helpers for processing different authmethods flows.
It's a hash-map, where key is an authentication method and value is a function, that takes the necessary user
secrets/keys and returns a function which accepts authmethod
and challenge
info and returns signed challenge answer.
You can provide your own signing functions or use existing helpers. Functions may be asynchronous.javascript1import * as wampyCra from 'wampy/wampcra';2import * as wampyCS from 'wampy/cryptosign';34wampy.setOptions({5 authPlugins: {6 // No need to process challenge data in ticket flow, as it is empty7 ticket: ((userPassword) => (() => userPassword ))(),8 wampcra: wampyCra.sign(secret),9 cryptosign: wampyCS.sign(privateKey)10 },11 authMode: 'auto'12});
manual
. Possible values: manual
|auto
. Mode of authorization flow. If it is set
to manual
- you also need to provide onChallenge callback, which will process authorization challenge. Or you
can set it to auto
and provide authPlugins (described above). In this case the necessary authorization flow
will be chosen automatically. This allows to support few authorization methods simultaneously.null
. Callback function.
It is fired when wamp server requests authentication during session establishment.
This function receives two arguments: auth method and challenge details.
Function should return computed signature, based on challenge details.
See Challenge Response Authentication section, WAMP Spec CRA,
Cryptosign-based Authentication section and WAMP Spec CS for more info.
This function receives welcome details as an argument.null
. Callback function. Fired on closing connection to wamp server.null
. Callback function. Fired on error in websocket communication or if error happens
during auto reconnection flow (as it can not be bound to explicit API calls).null
. Callback function. Fired every time on reconnection attempt.null
. Callback function. Fired every time when reconnection succeeded.
This function receives welcome details as an argument.null
. User provided WebSocket class. Useful in node environment.null
. User provided additional HTTP headers (for use in Node.js environment)null
. User provided WS Client Config Options (for use in Node.js environment).
See docs for WebSocketClient, tls.connect options.JsonSerializer
. User provided serializer class. Useful if you plan to use other encoders
instead of default json
.{ json: jsonSerializer }
. User provided hashmap of serializer instances for
using in Payload Passthru Mode. Allows to specify a few serializers and use them on per message/call basis.javascript1wampy.setOptions({2 reconnectInterval: 1000,3 maxRetries: 999,4 onClose: () => { console.log('See you next time!'); },5 onError: () => { console.log('Breakdown happened'); },6 onReconnect: () => { console.log('Reconnecting...'); },7 onReconnectSuccess: (welcomeDetails) => { console.log('Reconnection succeeded. Details:', welcomeDetails); }8});
Returns the status of last operation. This method returns an object with attributes:
code
is integer, and value > 0 means error.error
is Error instance of last operation. Check errors types exposed by wampy.reqId
is a Request ID of last successful operation. It is useful in some cases (call canceling for example).javascript1const defer = wampy.publish('system.monitor.update');2console.log(wampy.getOpStatus());3// may return4// { code: 1, error: UriError instance }5// or { code: 2, error: NoBrokerError instance }6// or { code: 0, error: null }
Returns the WAMP Session ID.
javascript1wampy.getSessionId();
Connects to wamp server. url parameter is the same as specified in Constructor.
Returns a Promise
that's either:
javascript1try {2 await wampy.connect();3} catch (e) {4 console.log('connection failed', e);5}67await wampy.connect('/my-socket-path');89const defer = wampy.connect('wss://socket.server.com:5000/ws');
Disconnects from wamp server. Clears all queues, subscription, calls. Returns a Promise
that's either:
javascript1await wampy.disconnect();
Aborts WAMP session and closes a websocket connection.
If it is called on handshake stage - it sends the abort
message to wamp server (as described in spec).
Also clears all queues, subscription, calls. Returns wampy instance back.
javascript1wampy.abort();
With Ticket-based authentication, the client needs to present the server an authentication ticket
-
some magic value to authenticate itself to the server. It could be a user password, an authentication token or
any other kind of client secret. To use it you need to provide "ticket" in "authmethods", "authid" and
the "onChallenge" callback as wampy instance options.
javascript1'use strict';23// Ticket authentication4wampy = new Wampy('wss://wamp.router.url', {5 realm: 'realm1',6 authid: 'joe',7 authmethods: ['ticket'],8 onChallenge: (method, info) => {9 console.log('Requested challenge with ', method, info);10 return 'joe secret key or password';11 }12});1314// Promise-based ticket authentication15wampy = new Wampy('wss://wamp.router.url', {16 realm: 'realm1',17 authid: 'micky',18 authmethods: ['ticket'],19 onChallenge: (method, info) => {20 return new Promise((resolve, reject) => {21 setTimeout(() => {22 console.log('Requested challenge with ', method, info);23 resolve('micky secret key or password');24 }, 2000);25 });26 }27});
Wampy.js supports challenge response authentication. To use it you need to provide the "authid" and the "onChallenge"
callback as wampy instance options. Also, Wampy.js supports wampcra
authentication method with a little helper
plugin "wampy/wampcra". Just import wampy/wampcra
and use provided methods as shown below.
javascript1'use strict';23import { Wampy } from 'wampy';4import * as wampyCra from 'wampy/wampcra'; // or import exact functions5import { w3cwebsocket as w3cws } from 'websocket';67// Manual authentication using signed message8wampy = new Wampy('wss://wamp.router.url', {9 ws: w3cws, // just for example in node.js env10 realm: 'realm1',11 authid: 'joe',12 authmethods: ['wampcra'],13 onChallenge: (method, info) => {14 console.log('Requested challenge with ', method, info);15 return wampyCra.signManual('joe secret key or password', info.challenge);16 }17});1819// Promise-based manual authentication using signed message20wampy = new Wampy('wss://wamp.router.url', {21 realm: 'realm1',22 authid: 'micky',23 authmethods: ['wampcra'],24 onChallenge: (method, info) => {25 return new Promise((resolve, reject) => {26 setTimeout(() => {27 console.log('Requested challenge with ', method, info);28 resolve(wampyCra.signManual('micky secret key or password', info.challenge));29 }, 2000);30 });31 }32});3334// Manual authentication using salted key and pbkdf2 scheme35wampy = new Wampy('wss://wamp.router.url', {36 realm: 'realm1',37 authid: 'peter',38 authmethods: ['wampcra'],39 onChallenge: (method, info) => {40 const iterations = 100;41 const keylen = 16;42 const salt = 'password salt for user peter';4344 console.log('Requested challenge with ', method, info);45 return wampyCra.signManual(wampyCra.deriveKey('peter secret key or password', salt, iterations, keylen), info.challenge);46 }47});4849// Automatic CRA authentication50wampy = new Wampy('wss://wamp.router.url', {51 realm: 'realm1',52 authid: 'patrik',53 authmethods: ['wampcra'],54 onChallenge: wampyCra.sign('patrik secret key or password')55});
Wampy.js supports cryptosign-based authentication. To use it you need to provide authid
, onChallenge
callback
and authextra
as wampy instance options. Also, Wampy.js supports cryptosign
authentication method
with a little helper plugin "wampy/cryptosign". Just import wampy/cryptosign
and use provided methods
as shown below.
The authextra
option may contain the following properties for WAMP-Cryptosign:
Field | Type | Required | Description |
---|---|---|---|
pubkey | string | yes | The client public key (32 bytes) as a Hex encoded string, e.g. 545efb0a2192db8d43f118e9bf9aee081466e1ef36c708b96ee6f62dddad9122 |
channel_binding* | string | no | If TLS channel binding is in use, the TLS channel binding type, e.g. "tls-unique" . |
challenge | string | no | A client chosen, random challenge (32 bytes) as a Hex encoded string, to be signed by the router. |
trustroot | string | no | When the client includes a client certificate, the Ethereum address of the trustroot of the certificate chain to be used, e.g. 0x72b3486d38E9f49215b487CeAaDF27D6acf22115 , which can be a Standalone Trustroot or an On-chain Trustroot |
*: channel_binding
is not supported yet. And may be supported only in node.js environment.
javascript1'use strict';23import { Wampy } from 'wampy';4import * as wampyCS from 'wampy/cryptosign';5// or you can import only the "sign" method6// import { sign } from 'wampy/cryptosign';78// Manual authentication using signed message9wampy = new Wampy('wss://wamp.router.url', {10 realm: 'realm1',11 authid: 'joe',12 authmethods: ['cryptosign'],13 authextra: {14 pubkey: '545efb0a2192db8d43f118e9bf9aee081466e1ef36c708b96ee6f62dddad9122'15 },16 onChallenge: (method, info) => {17 console.log('Requested challenge with ', method, info);18 return wampyCS.sign('joe secret (private) key')(method, info);19 }20});2122// Promise-based manual authentication using signed message23wampy = new Wampy('wss://wamp.router.url', {24 realm: 'realm1',25 authid: 'micky',26 authmethods: ['cryptosign'],27 authextra: {28 pubkey: '545efb0a2192db8d43f118e9bf9aee081466e1ef36c708b96ee6f62dddad9122'29 },30 onChallenge: (method, info) => {31 return new Promise((resolve, reject) => {32 setTimeout(() => {33 console.log('Requested challenge with ', method, info);34 resolve(wampyCS.sign('micky secret (private) key')(method, info));35 }, 2000);36 });37 }38});3940// Automatic CryptoSign authentication41wampy = new Wampy('wss://wamp.router.url', {42 realm: 'realm1',43 authid: 'patrik',44 authmethods: ['cryptosign'],45 authextra: {46 pubkey: '545efb0a2192db8d43f118e9bf9aee081466e1ef36c708b96ee6f62dddad9122'47 },48 onChallenge: wampyCS.sign('patrik secret (private) key')49});
If you server provides multiple options for authorization, you can configure wampy.js to automatically choose
required authorization flow based on authmethod
requested by server.
For this flow you need to configure the following options:
authid
. Authentication id to use in challengeauthmethods
. Supported authentication methodsauthextra
. Additional authentication optionsauthPlugins
. Authentication helpers for processing different authmethods challenge flowsauthMode
. Mode of authorization flow. Should be set to auto
onChallenge
. onChallenge callback. Is not used when authMode=auto
javascript1import { Wampy } from 'wampy';2import { sign as CraSign } from 'wampy/wampcra';3import { sign as CryptoSign } from 'wampy/cryptosign';45wampy = new Wampy('wss://wamp.router.url', {6 realm: 'realm1',7 authid: 'patrik',8 authmethods: ['ticket', 'wampcra', 'cryptosign'],9 authextra: { // User public key for Cryptosign-based Authentication10 pubkey: '545efb0a2192db8d43f118e9bf9aee081466e1ef36c708b96ee6f62dddad9122'11 },12 authPlugins: {13 ticket: ((userPassword) => (() => userPassword ))(),14 wampcra: CraSign(userSecret),15 cryptosign: CryptoSign(userPrivateKey)16 },17 authMode: 'auto',18 onChallenge: null19});
Subscribes for topicURI events.
Input Parameters:
Returns a Promise
that's either:
javascript1await wampy.subscribe('chat.message.received', (eventData) => { console.log('Received new chat message!', eventData); });23try {4 const response = await wampy.subscribe('some.another.topic',5 (eventData) => {6 console.log('Received topic event', eventData);7 }8 );9 console.log('Successfully subscribed to topic: ' + response.topic);1011} catch (e) {12 console.log('Subscription error:' + e.error);13}
Unsubscribe subscription from receiving events.
Parameters:
Returns a Promise
that's either:
javascript1const f1 = (data) => { console.log('this was event handler for topic') };2await wampy.unsubscribe('subscribed.topic', f1);34const defer = wampy.unsubscribe('chat.message.received');
Publish a new event to topic.
Parameters:
Returns a Promise
that's either:
javascript1await wampy.publish('user.logged.in');2await wampy.publish('chat.message.received', 'user message'); // will be sent as ['user message1']3await wampy.publish('chat.message.received', ['user message1', 'user message2']);4await wampy.publish('user.modified', { field1: 'field1', field2: true, field3: 123 });5await wampy.publish('chat.message.received', ['Private message'], { eligible: 123456789 });67try {8 await wampy.publish('user.modified', { field1: 'field1', field2: true, field3: 123 });9 console.log('User successfully modified');10} catch (e) {11 console.log('User modification failed', e.error, e.details);12}
Make an RPC call to topicURI.
Parameters:
Returns a Promise
that's either:
Important note on progressive call results:
For getting a progressive call results you need to specify progress_callback
in advancedOptions
.
This callback will be fired on every intermediate result. But the last one result or error
will be processed on promise returned from the .call()
. That means that final call result
will be received by call promise resolve
handler.
javascript1const result = await wampy.call('server.time');2console.log('Server time is ' + result.argsList[0]);34try {5 await wampy.call('start.migration');6 console.log('RPC successfully called');7} catch (e) {8 console.log('RPC call failed!', e.error);9}1011try {12 await wampy.call('restore.backup', { backupFile: 'backup.zip' });13 console.log('Backup successfully restored');14} catch (e) {15 console.log('Restore failed!', e.error, e.details);16}
Make an RPC progressive invocation call to topicURI.
Input parameters to this function are the same as described in the call() above. The difference is in the result.
Returns an object containing the result promise and the sendData
function
which is supposed to be used to send additional data chunks in bounds of the
initiated remote procedure call:
([payload], [advancedOptions])
:
javascript1let pc;2try {3 pc = ws.progressiveCall('sum.numbers', 1);4} catch (e) {5 console.log('RPC call failed!', e.error);6}78// Somewhere where you have additional data to be send to the procedure9try {10 pc.sendData(2);11 pc.sendData(3);12 pc.sendData(4);13 // This is final data chunk, so the Callee can understand that14 // no more input data will be send and it can finish its15 // calculations and send the result.16 // Note that nothing stops the Callee to send the17 // intermediate results if that makes sense.18 pc.sendData(5, { progress: false });19} catch (e) {20 console.log('RPC call send data failed!', e.error);21}222324// And here we are waiting for the final call result25const res = await pc.result26console.log("Res:", res); // Res: 15
RPC invocation cancelling.
Parameters:
Returns a Boolean
or throws an Error
:
true
if successfully sent canceling messageError
if some error occurredjavascript1const defer = wampy.call('start.migration');2defer3 .then((result) => console.log('RPC successfully called'))4 .catch((e) => console.log('RPC call failed!', e));56status = wampy.getOpStatus();78wampy.cancel(status.reqId);
RPC registration for invocation.
Parameters:
Returns a Promise
that's either:
Registered PRC during invocation will receive one hash-table argument with following attributes:
{ progress: true }
for intermediate results.RPC can return no result (undefined), any single value, array or hash-table object:
"progress": true
, which
indicates, that it's a progressive result, so there will be more results in the future.
Be sure to unset "progress" on last result message.javascript1const sqrt_f = function (data) { return { result: data.argsList[0]*data.argsList[0] } };23await wampy.register('sqrt.value', sqrt_f);45try {6 await wampy.register('sqrt.value', sqrt_f);7 console.log('RPC successfully registered');8} catch (e) {9 console.log('RPC registration failed!', e);10}
Also, wampy supports rpc with asynchronous code, such as some user interactions or xhr, using promises. For using this functionality in old browsers you should use polyfills, like es6-promise. Check browser support at can i use site.
javascript1const getUserName = () => {2 return new Promise((resolve, reject) => {3 /* Ask user to input his username somehow,4 and resolve promise with user input at the end */5 resolve({ argsList: userInput });6 });7};89wampy.register('get.user.name', getUserName);
Also, it is possible to abort rpc processing and throw error with custom application specific data. This data will be passed to caller onError callback.
Exception object with custom data may have the following attributes:
Note: Any other type of errors (like built in Javascript runtime TypeErrors, ReferenceErrors) and exceptions are caught by wampy and sent back to the client's side, not just this type of custom errors. In this case the details of the error can be lost.
javascript1const getSystemInfo = () => {23 // Application logic45 // for example, if you need to get data from db6 // and at this time you can't connect to db7 // you can throw exception with some details for client application89 const UserException = () => {10 this.error = 'app.error.no_database_connection';11 this.details = {12 errorCode: 'ECONNREFUSED',13 errorMessage: 'Connection refused by a remote host.',14 database: 'db',15 host: '1.2.3.4',16 port: 5432,17 dbtype: 'postgres'18 };19 this.argsList = ['Not able to connect to the database.'];20 this.argsDict = {};21 };2223 throw new UserException();24};2526await wampy.register('get.system.info', getSystemInfo);2728try {29 await wampy.call('get.system.info');30} catch (error) {31 console.log('Error happened', error);32}
RPC unregistration from invocations.
Parameters:
Returns a Promise
that's either:
javascript1await wampy.unregister('sqrt.value');23try {4 wampy.unregister('sqrt.value');5 console.log('RPC successfully unregistered');6} catch (e) {7 console.log('RPC unregistration failed!', e);8}
During wampy instance lifetime there can be many cases when error happens: some
made by developer mistake, some are bound to WAMP protocol violation, some came
from other peers. Errors that can be caught by wampy instance itself are stored
in opStatus.error
, while others are just thrown.
This allows, for example, convenient handling of different types of errors:
javascript1import {Wampy, Errors} from 'wampy';2const wampy = new Wampy('/ws/', { realm: 'AppRealm' });34try {5 await wampy.call('start.migration');6 console.log('RPC successfully called');7} catch (error) {8 console.log('Error happened!');9 if (error instanceof Errors.UriError) {10 // statements to handle UriError exceptions11 } else if (error instanceof Errors.InvalidParamError) {12 // statements to handle InvalidParamError exceptions13 } else if (error instanceof Errors.NoSerializerAvailableError) {14 // statements to handle NoSerializerAvailableError exceptions15 } else {16 // statements to handle any unspecified exceptions17 }18}
Wampy package exposes the following Error
classes:
For errors attributes look at src/errors.js file.
Wampy.js supports custom attributes in advancedOptions
for protocol extensibility as defined in WAMP specification section 3.1.
Any option matching the pattern _[a-z0-9_]{3,}
(starting with underscore, followed by at least 3 alphanumeric characters or underscores) will be passed through as-is to the WAMP router. This allows for custom extensions and router-specific features.
Supported in:
Examples:
javascript1// Custom tracking and priority attributes2await wampy.call('api.process.data', { data: 'test' }, {3 _tracking_id: 'req_12345',4 _priority: 'high',5 _custom_auth: 'bearer_token_xyz',6 timeout: 50007});89// Custom routing hints10await wampy.call('distributed.service', payload, {11 _route_to_region: 'us-west',12 _load_balancer_hint: 'sticky_session',13 _retry_policy: 'exponential'14});
Note: Custom attributes are only passed for methods that support them. Standard WAMP options (like timeout
, disclose_me
, etc.) are handled separately and don't need the underscore prefix.
From v5.0 version there is option to provide custom serializer.
Custom serializer instance must meet a few requirements:
encode (data)
method, that returns encoded datadecode (data)
method, that returns decoded dataprotocol
string property, that contains a protocol name. This name is concatenated with
"wamp.2." string and is then passed as websocket subprotocol http header.isBinary
boolean property, that indicates, is this a binary protocol or not.Take a look at json-serializer.js or msgpack-serializer.js as examples.
Starting from v6.2.0 version you can pass additional HTTP Headers and TLS parameters to underlying socket connection
in node.js environment (thnx websocket
library). See example below. For wsRequestOptions
you can pass any option,
described in tls.connect options documentation.
javascript1const Wampy = require('wampy').Wampy;2const w3cws = require('websocket').w3cwebsocket;34wampy = new Wampy('wss://wamp.router.url:8888/wamp-router', {5 ws: w3cws,6 realm: 'realm1',7 additionalHeaders: {8 'X-ACL-custom-token': 'dkfjhsdkjfhdkjs',9 'X-another-custom-header': 'header-value'10 },11 wsRequestOptions: {12 ca: fs.readFileSync('ca-crt.pem'),13 key: fs.readFileSync('client1-key.pem'),14 cert: fs.readFileSync('client1-crt.pem'),15 host: 'wamp.router.url',16 port: 8888,17 rejectUnauthorized: false, // this setting allow to connect to untrusted (or self signed) TLS certificate,18 checkServerIdentity: (servername, cert) => {19 // A callback function to be used (instead of the builtin tls.checkServerIdentity() function)20 // when checking the server's hostname (or the provided servername when explicitly set)21 // against the certificate. This should return an <Error> if verification fails.22 // The method should return undefined if the servername and cert are verified.23 if (servername !== 'MyTrustedServerName') {24 return new Error('Bad server!');25 }26 }27 }28});
Wampy.js uses mocha and chai for tests and c8/istanbul for code coverage. Wampy sources are mostly all covered with tests! :)
bash1# use standard npm test command2> npm test3# or only some tests4> npm run test:node5> npm run test:node-no-crossbar6> npm run test:browser78# for code coverage report run9> npm run cover10# and then open coverage/lcov-report/index.html
Wampy.js library is licensed under the MIT License (MIT).
Copyright (c) 2014 Konstantin Burkalev
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.
Thanks JetBrains for support! Best IDEs for every language!