I'm beginning with websockets and I'm very confused with the number libraries and configuration options. I just want to setup a project where a node.js server calls a method in python/django and when the last has finished, it transfers the result back to the node.js server. This is what I have so far:
Nodes.js AMQP from this tutorial:
var conn = amqp.createConnection();
conn.on('ready', function(){
var exchange = conn.exchange('?1', {'type': 'fanout', durable: false}, function() {
exchange.publish('?2', {add: [1,2]});
});
});
Django Celery from this tutorial:
from celery.decorators import task
@task()
def add(x, y):
return x + y
I don't know if this is the way to go, and I would be glad if someone could shed a light on this issue.
--- EDIT
I succeed in making a simple string transfer using AMQP:
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()
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
Is there any solution for serializing complex objects? Am I missing something?
the body must be a byte array!
you must binary serialize your objects. but nods.jss and pythons are may be incompatible.
python: pickle node.js: rucksack on of...
a quick workaround could be to join your array to a string like this:
test.py
...
body = ';'.join(["a","b","c"]) # which will result in this string 'a;b;c'
...
and in your app.js split that string to an array.
...
result = stringResult.split(';'); //stringResult is 'a;b;c'
...
another option could be to do that asyncronous-task in node.js with 'async'