rabbit.js icon indicating copy to clipboard operation
rabbit.js copied to clipboard

REQ/REP socket - provide orderless interface

Open xaka opened this issue 11 years ago • 6 comments

Hi there,

First all I wanted to say the API is so amazing, clear and simple to use. Great job! And second of all the REQ/REP with explicit ordering is very limiting for development, especially in Node.js where everything is async, but it is what it is. In my case I don't care about the order so what I'd like to see is the following API:

For client:

socket.call('method', function (error, value) {
  ...
});

For server:

socket.on('method', function (value, callback) {
  ...
  callback([error], result);
  ...
});

And let the library handle mapping between requests, responses and correlation IDs.

I could do it by my self, but library doesn't provide an API to extend it or add new socket types.

xaka avatar Oct 14 '14 05:10 xaka

Just to check I understand what you're after: instead of

var req = ctx.socket('REQUEST');
req.connect("questions");
req.write("foobar?");
var answer = req.read();

you'd rather have (something like):

var req = ctx.socket('REQUEST');
req.connect('questions')
req.ask('foobar?', function(answer) {
  // ... something
});

Is that right? I can see why that's somewhat easier, given that read isn't blocking.

squaremo avatar Oct 14 '14 07:10 squaremo

That's right. I think as it's RPC, it should look like a normal asynchronous function call and handling where client/server don't care about the order.

xaka avatar Oct 14 '14 13:10 xaka

i would like to see something along these lines, as well. i'm just now getting to the point where i realize the need for request/response, and having this callback based API would be nice.

mxriverlynn avatar Oct 30 '14 15:10 mxriverlynn

I don't think this would be overly difficult but I might not get to it very soon -- anyone fancy giving it a go?

squaremo avatar Nov 03 '14 21:11 squaremo

I might, but my concern is that it doesn't fit well into current behave-like-a-Socket idea. It goes kind of against the flow. What we could do though, as a first step, it to make sequential behavior to be optional i.e. when you read from socket, you should expect to get messages in random order. That would unblock the ability to build RPC-like servers/clients on top of it. What about adding sequential to list of options with default value being true for backward compatibility?

xaka avatar Nov 03 '14 21:11 xaka

when you read from socket, you should expect to get messages in random order.

I don't think that would help -- you wouldn't know what is an answer to what. It really has to be done in the machinery behind a socket.

It's easy enough to do without altering the machinery, if you don't mind the callbacks being called in order (i.e., later callbacks waiting for earlier ones, even if they arrive first). Otherwise, it is a bit more difficult than I indicated above.

squaremo avatar Nov 03 '14 23:11 squaremo