user2882721
user2882721

Reputation: 231

passing a json object to zmq server

Hi i basically wrote a client and a server using the zmq module.I was able to send a normal string as a input to the server using my client and the data displayed well.but when i passed a JSON object it is getting printed differently.Below is my client code

CLIENT CODE

var zmq = require('zmq');
// socket to talk to server
console.log("Connecting to hello world server...");
var requester = zmq.socket('req');

var x = 0;
requester.on("message", function(reply) {

console.log("Received reply for client from server", ": [", reply.toString(), ']');
x += 1;
if (x === 5) {
requester.close();
 process.exit(0);

} });

requester.connect("tcp://localhost:7000");

for (var i = 0; i < 5; i++) {

console.log("Sending request", i, '...');
requester.send({"AdvisorId" : "71864", "Phone": "952-921-4972"});
}

process.on('SIGINT', function() {
requester.close();
});

SERVER CODE

// Hello World server
// Binds REP socket to tcp://*:7000
// Expects "Hello" from client, replies with "world"

var zmq = require('zmq');

// socket to talk to clients
var responder = zmq.socket('rep');

responder.on('message', function(request) {

 console.log("Received request: [", request, "]");
console.log(typeof request)

 // do some 'work'
 setTimeout(function() {

// send reply back to client.
responder.send("world");
 }, 1000);
});

responder.bind('tcp://*:7000', function(err) {
if (err) {
console.log(err);
 } else {

console.log("Listening on 7000...");
 }
});

process.on('SIGINT', function() {
responder.close();
});

request data in server

Received request: [ <SlowBuffer 5b 6f 62 6a 65 63 74 20 4f 62 6a 65 63 74 5d> ]

whereas the data that i am sending is {"AdvisorId" : "71864", "Phone": "952-921-4972"} which is a JSON object.Im stuck here any help regarding this will be much helpfull.

Upvotes: 2

Views: 4906

Answers (2)

kiran Sp
kiran Sp

Reputation: 153

var jsonObject={"AdvisorId" : "71864", "Phone": "952-921-4972"} ;
var stringObject=JSON.stringify(jsonObject);
requester.send(stringObject);

This will work..!!

Upvotes: 4

raffian
raffian

Reputation: 32076

ZeroMq supports string and byte[] data, not objects.

Try this:

requester.send("{AdvisorId" : "71864", "Phone": "952-921-4972}");

Upvotes: 0

Related Questions