Search code examples
node.jsautobahn

Autobahn identify subscription


When creating a websockets connection how can I determine the name of the subscription in the event function.

For example if I have three subscriptions I wish to be able to identify which once triggered the event.

var autobahn = require('autobahn');

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

connection.onopen = function (session) {

   function onevent(args) {
      console.log("Colour is: ", ?????);
   }

   ["Red", "Green", "Brown"].forEach(function(colour) {
     session.subscribe(colour, onevent);
   }


};

connection.open();

Solution

  • Your onevent function actually receives three arguments: args, kwargs and details, where the first two are message payload (an array and an object). The details object contains what you are looking for: the subscription topic.