Skip to content

Instantly share code, notes, and snippets.

@johngeorgewright
Last active November 2, 2015 22:52
Show Gist options
  • Save johngeorgewright/c20a59f196a25f9b84b5 to your computer and use it in GitHub Desktop.
Save johngeorgewright/c20a59f196a25f9b84b5 to your computer and use it in GitHub Desktop.
Register a WAMP procedure on more than one service. The idea being that a backup version of the service will register automatically if the primary one goes down.
import autobahn from 'autobahn';
const RPC_ADD2 = 'com.myapp.add2';
let connection = new autobahn.Connection({
url: 'ws://127.0.0.1:9000/ws',
realm: 'realm1',
use_es6_promises: true
});
connection.onopen = onOpen;
connection.open();
function add2(args) {
return args[0] + args[1];
}
async function attach(session) {
await session.register(RPC_ADD2, add2);
session.log('Registered');
}
async function onOpen(session) {
try {
let id = await session.call('wamp.registration.lookup', [RPC_ADD2]);
let reg;
if (id) {
reg = await session.call('wamp.registration.get', [id]);
}
if (reg && reg.uri === RPC_ADD2) {
session.log('Waiting...');
session.subscribe('wamp.registration.on_unregister', () => attach(session));
} else {
await attach(session);
}
} catch (error) {
session.log(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment