squaremo / rabbit.js

Messaging in Node.JS made easy using RabbitMQ
Other
1.52k stars 142 forks source link

Running REQ/REP as an RPC #118

Open jborden13 opened 8 years ago

jborden13 commented 8 years ago

Hi, I'm attempting to run as an RPC using the Request/Reply socket types. Everything is working great, until it is time to respond back with a message. I've tried responding directly back to the replyTo, but I'm getting:

Error: Channel closed by server: 403 (ACCESS-REFUSED) with message "ACCESS_REFUSED - queue name 'amq.gen-sMgjS-5c4qvKKR2ArMhKTA' contains reserved prefix 'amq.*'

Which makes sense that it's a reserved queue name. But if I can't write to it, how do I reply?

Request:

context.on("ready", function () {
    var request = context.socket("REQ", {expiration: TIMEOUT}); 
     request.setEncoding('utf8');
     request.on("data", function (message) {
      process.nextTick(function () {
        request.close();
        res.send(message, {});
      });
    });
     request.connect("RPCQueueName", function () {
      var message = JSON.stringify(req.body);
      console.log(request);
      request.write(message, "utf8");
    });
  });

Reply:

reply.connect(json.ReplyTo, function () {
                            reply.setEncoding('utf8');
                            reply.write(JSON.stringify({ Response: 'response' }), "utf8");
                        });

I've tried messing with the replyTo and correlationId to no avail. Do you know of any ways to reply directly to an AMPQ queue - or another method?

In your documentation you wrote:

If you are replying via AMQP or STOMP, be sure to follow the convention of sending the response to the queue given in the 'replyTo' property of the request message, and copying the 'correlationId' property from the request in the reply. If you are requesting via AMQP or STOMP, at least supply a replyTo, and consider supplying a correlationId.

Can you provide an example of how to correctly supply the replyTo and correlationId for the request and how to best follow the convention of sending the response to the queue? I've been scouring the docs for days with no luck. Thanks!