kaazing publishBasic pass json data and catch it in node.js
Problem
kaazing Publishbasic Pass Json data and catch it in node.js
Below is the code to publish Data to RabbitMQ via kaazing AMQP librarry
var body = new ByteBuffer();
body.putString("new_bidder", Charset.UTF8);
body.flip();
var headers = {};
publishChannel.publishBasic(body, headers, consumeExchange, "server.aa.bb", false, false);
Here is the code to catch the data in node.js
q.subscribe(function (message) {
// Print messages to stdout
var msg = message.toString('UTF-8');
console.log(msg.length);
// console.log(message);
})
Problem is that publishBasic function expect body in bytes and i want to pass json and get that json in node.js
any help will be hightly appreciated.
Problem courtesy of: Jack Daniel's
Solution
Just use JSON.stringify.
var myObject = {
bidder : "new_bidder",
property2 : property2_value,
};
var body = new ByteBuffer();
body.putString(JSON.stringify(myObject), Charset.UTF8);
body.flip();
publishChannel.publishBasic(body, headers, consumeExchange, "server.aa.bb", false, false);
And in Node.JS, you use JSON.parse:
q.subscribe(function (message) {
var msg = message.toString('UTF-8');
// Print messages to stdout
console.log(msg);
// now convert back to JSON object so you can use in your code
var myObject = new Object();
myObject = JSON.parse(msg);
console.log(msg.bidder);
})
Solution courtesy of: Axel
Discussion
View additional discussion.