REQ/REP socket - provide orderless interface
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.
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.
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.
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.
I don't think this would be overly difficult but I might not get to it very soon -- anyone fancy giving it a go?
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?
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.