2
votes

I have written an application which is nodejs (main app) and python (client) app that i want to communicate with each other using zmq Router,Dealer pattern.

the problem is i could not read the messages sent from clients to nodejs (router) app. its encoded some how.

the code is as simple as below:

var responder = zmq.socket('router');
responder.on('message', function(request) {
    console.log(request);
    // i could not read the messages here.its obfuscated 
});

responder.bind('tcp://127.0.0.1:8000', function(err) {
    if (err) {
        console.log(err);
    } else {
        console.log('Listening on 8000...');
    }
});

python:

socket = context.socket(zmq.DEALER)
        socket.connect("tcp://127.0.0.1:8000")
        socket.send('blaaaa')
        print 'message sent!'
2

2 Answers

2
votes

If you wish to use the DEALER/ROUTER sockets, then the message is actually given as the second argument for the callback function.

var responder = zmq.socket('router');
responder.on('message', function(header, body) {
    console.log(body.toString('utf8'));
});

The message is in the format of a Buffer, but you can turn it into a string using .toString(encoding);

The header contains an identity, this allows you to later route the response/answer back to the correct sender/requester that made the original request.

0
votes

For your application, PUSH-PULL seems more appropriate:

var zmq = require('zmq');

var responder = zmq.socket('pull');
responder.on('message', function(request) {
    console.log(request.toString());
    // Use `toString` to convert Buffer to string
});

responder.bind('tcp://127.0.0.1:8000', function(err) {
    if (err) {
        console.log(err);
    } else {
        console.log('Listening on 8000...');
    }
});

import zmq

context = zmq.Context()
socket = context.socket(zmq.PUSH)
socket.connect("tcp://127.0.0.1:8000")
socket.send('blaaaa')
print 'message sent!'