简体   繁体   中英

How to deliver a response to a client after a Node.js server gets a message from a AMQP queue?

When a client makes a get request to '/test' a simple string is exchanged between node.js and python via AMQP, but I don't know how to transmit the response back to the client (since the process is async).

test.py

 import pika
 connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
 channel = connection.channel()

 channel.queue_declare(queue='task_queue', durable=True)

 print ' [*] Waiting for messages. To exit press CTRL+C'

 def callback(ch, method, props, body):
     print " [x] Received %r" % (body,)
     response = body + " MODIFIED"
     #response = get_a_concept()
     print " [x] Done"
     ch.basic_publish(exchange='',
                 routing_key=props.reply_to,
                 properties=pika.BasicProperties(correlation_id = \
                                                 props.correlation_id),
                 body=str(response))
     ch.basic_ack(delivery_tag = method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,
                  queue='task_queue')

channel.start_consuming()

app.js

 var connection = amqp.createConnection({ host: 'localhost' });
 connection.addListener('ready', function() {


var exchange = connection.exchange('', {
    'type' : 'direct',
    durable : false
        }, function() {

    var queue = connection.queue('incoming', {
        durable : false,
        exclusive : true }, function() {
        queue.subscribe(function(msg) {

           // got response here, how to transmit it to the node that made the exchange?
            console.log("received message: ");
            console.log(msg.data.toString());
        });

      });

    });
});

User request makes a publish to python, but how to reply it back to the user once it's finished?

app.get('/test', loadUser, function(req, res) {

console.log("sent");
exchange.publish('task_queue', "funciona!", {
    'replyTo' : 'incoming'
});

res.redirect('/home'); 

});

(Note: I'm not sure if this is the best implementation. Hints, suggestions welcomed!)

I solved it as follows:

The requesting side sets the reply-to and correlation-id headers when sending a message and stores the information needed to process the reply (in my case, NodeJS, a callback) in a list with the correlation-id as index.

The responding side publishes to a direct exchange with reply-to as routing key and sets the correlation-id on the message.

Now when the message arrives back at the requester, it simply gets (and removes) the needed info from the list and handles the response.

Edit: Ofcourse there's some more work to be done if you want to handle timeouts, etc, but that all depends on your usecase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM