JustinTulloss / zeromq.node

Node.js bindings to the zeromq library
MIT License
1.65k stars 284 forks source link

How to receive a reply when using REQ socket? #583

Open pronebird opened 7 years ago

pronebird commented 7 years ago

I am looking at some py bindings for zeromq and what they do for REQ socket is:

socket.send ("Hello")
message = socket.recv()

.recv() is undefined in zeromq.node and I don't see an alternative for that either. Would you mind pointing me in the right direction?

pronebird commented 7 years ago

OK I figured that message event is fired instead.

req_socket.on('message', function (message) {
  console.log('<- ' + message.toString('utf8'));
});
pronebird commented 7 years ago

Reopening this because it's HIGHLY inconvenient to work with REQ socket this way. I'll explain why. If you have RPC interface that takes data, performs request and returns response, you have to do some magic dance to funnel received response back to RPC implementation, also you have to take care of execution order, etc. It would be great if there was a way to send request with callback that would be called with response, i.e:

socket.send(req, function (resp) {
   // do something
});
patrickjane commented 7 years ago

I agree to you, in that it would need extra code/multipart messages to match responses to previous requests in the 'message'-event callback.

Your suggestion looks much better.