WAMP: Difference between revisions

From air
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 9: Line 9:


==[[Node.js]]==
==[[Node.js]]==
===Install Crossbar (the Router)===
http://autobahn.ws/js/gettingstarted.html
http://autobahn.ws/js/programming.html
* http://crossbar.io/


<pre>
<pre>
pip install crossbar[tls,msgpack,manhole,system]
npm install autobahn
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/gettingstarted.html
* http://autobahn.ws/js/programming.html


<pre>
<pre>
npm install -g autobahn
export NODE_PATH=/usr/local/lib/node_modules
</pre>



var autobahn = require('autobahn')
<pre>
var autobahn = require('autobahn');


var connection = new autobahn.Connection({
var connection = new autobahn.Connection({
url: 'ws://127.0.0.1:9000/',
url: 'ws://127.0.0.1:8080/ws',
realm: 'realm1'
realm: 'realm1'}
});
);


connection.onopen = function (session) {
connection.onopen = function (session) {


// 1) subscribe to a topic
// SUBSCRIBE to a topic and receive events
//
function onevent(args) {
console.log("Event:", args[0]);
function onhello (args) {
var msg = args[0];
console.log("event for 'onhello' received: " + msg);
}
}
session.subscribe('com.myapp.hello', onevent);
session.subscribe('com.example.onhello', onhello).then(
function (sub) {
console.log("subscribed to topic 'onhello'");
},
function (err) {
console.log("failed to subscribed: " + err);
}
);


// 2) publish an event
session.publish('com.myapp.hello', ['Hello, world!']);


// 3) register a procedure for remoting
// REGISTER a procedure for remote calling
//
function add2(args) {
return args[0] + args[1];
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.myapp.add2', add2);
session.register('com.example.add2', add2).then(
function (reg) {

// 4) call a remote procedure
console.log("procedure add2() registered");
},
session.call('com.myapp.add2', [2, 3]).then(
function (res) {
function (err) {
console.log("Result:", res);
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();
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

Extra

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

Open http://localhost:8080/

Install Autobahn (The Node.js client)

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