Search code examples
rabbitmqrpcstomp

Rabbitmq + web stomp plugin with rpc - reply-to


I'm trying to perform an RPC with RabbitMQ's STOMP adapter. As the client lib I'm using the STOMP over WebSocket (https://github.com/jmesnil/stomp-websocket/) library.

From the documentation (http://www.rabbitmq.com/stomp.html#d.tqd) I see that I have to set the reply-to header. I've done that by specifying something like "reply-to: /temp-queue/foo" and I saw in my server-side client (node-amqp) that the replyTo header is set correctly (example: replyTo: '/reply-queue/amq.gen-w2jykNGp4DNDBADm3C4Cdx'). Still in my server-side client, I can reply to the message just by publishing a message to "/reply-queue/amq.gen-w2jykNGp4DNDBADm3C4Cdx".

However, how do I get this reply it in my client code where the RPC call was initiated? The documentation states "SEND and SUBSCRIBE frames must not contain /temp-queue destinations (...) subscriptions to reply queues are created automatically."

So, how do I subscribe to the reply-to queue? How can I get the results of RPC calls?

Thanks in advance.


Solution

  • The answer is:

    When you receive the rpc call in the server worker you get the header replyTo. That header comes like:

    replyTo: '/reply-queue/[queue_name]'
    

    for example: replyTo:'/reply-queue/amqp.fe43gggr5g54g54ggfd_'

    The trick is:

    • you have to parse it and only answer to the queue_name [for example: amqp.fe43gggr5g54g54ggfd_]
    • You have to answer to the default exchange and not to any other exchange

    Example of an answer in nodejs:

    function onRpcReceived(message, headers, deliveryInfo, m) {
        var reply_to = m.replyTo.toString().substr(13, m.replyTo.toString().length);
    
        connection.publish(reply_to, {response:"OK", reply:"The time is 13h35m"}, {
                    contentType:'application/json',
                    contentEncoding:'utf-8',
                    correlationId:m. correlationId
        });
    }
    

    Now i just wonder why the web-stomp-plugin adds the /reply-queue/ string to the attribute "replyTo" on the header instead of only add the queue name....! If someone knows the reason i would like to know.