1
votes

I have a nodejs app which listen for messages from clients (python app). the pattern i used for communication over zmq is REQ/REP pattern. the Main app should get messages from many clients. it will not reply to them, just get messages. the problem is the main app will only get the first message and the next messages are not shown in nodejs app console.

in other words every time i start nodejs app i only get one message.

here is my code:

Nodejs app

var responder = zmq.socket('rep');
responder.on('message', function(request) {
    console.log(request);
    //here, it seems this function will be called just once!
});

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

python (client) part:

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

python part is inside a function. i could see the output of "message sent!" in python console(i mean many 'message sent!'). but i could not see the messages in nodejs app.just the first message is seen in the console of nodejs.

1

1 Answers

3
votes

When using the REQ/REP-pattern you actually need to respond to a request before you are given the next request - you will only handle one request at the time.

var responder = zmq.socket('rep');
responder.on('message', function(request) {
    console.log(request);
    responder.send('Here comes the reply!');
});

Respond, and you will receive the next one. If you do not wish to respond, then you need to choose some other socket pair than req/rep - ex: push/pull or maybe look at xreq/xrep (router/dealer) if you wish to handle multiple requests at the same time.

If in doubt, look up the send/receive pattern for each socket type at http://api.zeromq.org/2-1:zmq-socket