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

Single callback queue for every RPC request

Open utestmig opened this issue 11 years ago • 3 comments

How can I use a single callback queue for every RPC request, as mentioned in the RabbitMQ docs: "In the method presented above we suggest creating a callback queue for every RPC request. That's pretty inefficient, but fortunately there is a better way - let's create a single callback queue per client."

Currently, I'm doing the following:

    var deferred = q.defer();
    var queueName = 'tests.rpc';
    var request = context.socket('REQ', {expiration: timeout});
    request.on('data', function (inMessage) {
      var data = JSON.parse(inMessage);
      deferred.resolve(data);
    });
    request.connect(queueName, function () {
      var message = JSON.stringify(parameters);
      request.write(message, 'utf8');
    });
   return deferred.promise;
  • Should I only create one 'REQ' socket?
  • Would I continue to listen to 'data'? If so, how will the correlationId get resolved? This is being generated in the 'write' function.

utestmig avatar Sep 03 '14 15:09 utestmig

Hello, sorry to miss this one for a while.

The RabbitMQ docs you quote are saying "don't create a queue per request, create a queue per client". It's the latter which rabbit.js does.

squaremo avatar Oct 03 '14 17:10 squaremo

Right, I believe we are saying the same thing. I basically copied/pasted what the docs said "let's create a single callback queue per client".

I'm still wondering about:

  • Should I only create one 'REQ' socket?
  • Would I continue to listen to 'data'? If so, how will the correlationId get resolved? This is being generated in the 'write' function.

utestmig avatar Oct 06 '14 15:10 utestmig

I see, you want to multiplex requests from different sources over the socket (or create multiple sockets).

The way rabbit.js works, you would have to know the order in which you requested things; internally, it assigns each a correlationId (as you mention), then it emits the answers in the order the requests came in.

So, as a sketch, you could push each request (as a promise, if you want to keep the scheme in the example above) into a queue (= array), and shift from the queue when the answer came back.

squaremo avatar Oct 06 '14 18:10 squaremo