How can I get JSON data from the server by using Autobhan(Crossbar)

261 Views Asked by At

I got this as below from backend developer:

 session.publish('com.test.temp', [
        temp,
        humidity,
        componentId,
        componentType
     ], {}, { exclude_me: true });

So I succeed in a handshake with the server using WebSocket:

 var ws = new WebSocket('ws://1server address','example.json');

  ws.onopen = function () {
      console.log('websocket is connected ...')

      ws.send('connected')
  }

  ws.onmessage = function (ev) {
      console.log(ev);
  }

But I could not see any data in the console.log(ev)

How can I get JSON data from the server??

1

There are 1 best solutions below

0
On

I find a way to get data from the crossbar.

I was supposed to use autobahn library.

This is my solution:

var connection = new autobahn.Connection({ url : 'server url', realm: 'realm1'
});

connection.onopen = function (session) {

console.log('websocket is connected ...')

session.subscribe('com.example.example', function(message) {
 console.log(message);

 }

}

connection.open();

I hope this answer will be helpful to someone.