WAMP: Difference between revisions
Jump to navigation
Jump to search
(4 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
==[[Node.js]]== |
==[[Node.js]]== |
||
===Install Crossbar (the Router)=== |
|||
⚫ | |||
* http://crossbar.io/ |
|||
<pre> |
<pre> |
||
pip install crossbar[tls,msgpack,manhole,system] |
|||
⚫ | |||
crossbar version |
|||
crossbar init --template hello:nodejs --appdir hello |
|||
cd hello |
|||
crossbar start |
|||
</pre> |
</pre> |
||
Open http://localhost:8080/ |
|||
===Install Autobahn (The Node.js client)=== |
|||
* http://crossbar.io/docs/Getting-started-with-NodeJS/ |
|||
⚫ | |||
* http://autobahn.ws/js/programming.html |
|||
<pre> |
<pre> |
||
⚫ | |||
export NODE_PATH=/usr/local/lib/node_modules |
|||
</pre> |
|||
⚫ | |||
<pre> |
|||
⚫ | |||
var connection = new autobahn.Connection({ |
var connection = new autobahn.Connection({ |
||
url: 'ws://127.0.0.1:8080/ws', |
|||
realm: 'realm1'} |
|||
); |
|||
connection.onopen = function (session) { |
connection.onopen = function (session) { |
||
// |
// SUBSCRIBE to a topic and receive events |
||
// |
|||
⚫ | |||
function onhello (args) { |
|||
var msg = args[0]; |
|||
console.log("event for 'onhello' received: " + msg); |
|||
} |
} |
||
session.subscribe('com. |
session.subscribe('com.example.onhello', onhello).then( |
||
⚫ | |||
console.log("subscribed to topic 'onhello'"); |
|||
}, |
|||
⚫ | |||
console.log("failed to subscribed: " + err); |
|||
} |
|||
); |
|||
⚫ | |||
⚫ | |||
// |
// REGISTER a procedure for remote calling |
||
// |
|||
⚫ | |||
function add2 (args) { |
|||
var x = args[0]; |
|||
var y = args[1]; |
|||
console.log("add2() called with " + x + " and " + y); |
|||
return x + y; |
|||
} |
} |
||
session.register('com. |
session.register('com.example.add2', add2).then( |
||
function (reg) { |
|||
console.log("procedure add2() registered"); |
|||
}, |
|||
⚫ | |||
function ( |
function (err) { |
||
console.log(" |
console.log("failed to register procedure: " + err); |
||
} |
} |
||
); |
); |
||
// PUBLISH and CALL every second .. forever |
|||
// |
|||
var counter = 0; |
|||
setInterval(function () { |
|||
⚫ | |||
// |
|||
⚫ | |||
console.log("published to 'oncounter' with counter " + counter); |
|||
// CALL a remote procedure |
|||
// |
|||
⚫ | |||
function (res) { |
|||
console.log("mul2() called with result: " + res); |
|||
}, |
|||
function (err) { |
|||
if (err.error !== 'wamp.error.no_such_procedure') { |
|||
console.log('call of mul2() failed: ' + err); |
|||
} |
|||
} |
|||
); |
|||
counter += 1; |
|||
}, 1000); |
|||
}; |
}; |
||
connection.open(); |
connection.open(); |
||
</pre> |
</pre> |
||
Latest revision as of 21:20, 16 November 2014
WAMP (Web Application Messaging Protocol) is an open WebSocket subprotocol that provides two application messaging patterns in one unified protocol: Remote Procedure Calls + Publish-Subscribe
- http://wamp.ws/spec/
- https://github.com/tavendo/WAMP/blob/master/spec/basic.md
- https://github.com/tavendo/WAMP/blob/master/spec/advanced.md
Extra
- WAMP with Arduino Yun http://tavendo.com/blog/post/arduino-yun-with-autobahn/
Node.js
Install Crossbar (the Router)
pip install crossbar[tls,msgpack,manhole,system] crossbar version crossbar init --template hello:nodejs --appdir hello cd hello crossbar start
Install Autobahn (The Node.js client)
- http://crossbar.io/docs/Getting-started-with-NodeJS/
- http://autobahn.ws/js/gettingstarted.html
- http://autobahn.ws/js/programming.html
npm install -g autobahn export NODE_PATH=/usr/local/lib/node_modules
var autobahn = require('autobahn'); var connection = new autobahn.Connection({ url: 'ws://127.0.0.1:8080/ws', realm: 'realm1'} ); connection.onopen = function (session) { // SUBSCRIBE to a topic and receive events // function onhello (args) { var msg = args[0]; console.log("event for 'onhello' received: " + msg); } session.subscribe('com.example.onhello', onhello).then( function (sub) { console.log("subscribed to topic 'onhello'"); }, function (err) { console.log("failed to subscribed: " + err); } ); // REGISTER a procedure for remote calling // function add2 (args) { var x = args[0]; var y = args[1]; console.log("add2() called with " + x + " and " + y); return x + y; } session.register('com.example.add2', add2).then( function (reg) { console.log("procedure add2() registered"); }, function (err) { console.log("failed to register procedure: " + err); } ); // PUBLISH and CALL every second .. forever // var counter = 0; setInterval(function () { // PUBLISH an event // session.publish('com.example.oncounter', [counter]); console.log("published to 'oncounter' with counter " + counter); // CALL a remote procedure // session.call('com.example.mul2', [counter, 3]).then( function (res) { console.log("mul2() called with result: " + res); }, function (err) { if (err.error !== 'wamp.error.no_such_procedure') { console.log('call of mul2() failed: ' + err); } } ); counter += 1; }, 1000); }; connection.open();
Node-RED
TODO