Thanks Micheil, Bika,
I succeed in making a simple string transfer using AMQP:
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) {
console.log("received message: ");
console.log(msg.data.toString());
});
});
exchange.publish('task_queue', "it works!", {
'replyTo' : 'incoming'
});
});
});
---
Still, I'm not sure if this is the best implementation, I'm not even
using queue.bind() method here. The problem arises when I try to pass
a complex object (json or even a simple array). Changing this line
body= (["a","b","c"])#str(response)) causes the following error:
Traceback (most recent call last):
File "test.py", line 56, in <module>
channel.start_consuming()
File "/Library/Python/2.7/site-packages/pika/adapters/
blocking_connection.py", line 293, in start_consuming
(...)
File "/Library/Python/2.7/site-packages/pika/simplebuffer.py", line
62, in write
self.buf.write(data)
TypeError: must be string or read-only character buffer, not list
But that I will check in some python list. If someone has any comment
don't hesitate!
Cheers,
Cassio