Google Groups

Re: How to transfer objects between Node.js and Django?


Cassio Melo Apr 27, 2012 8:35 AM
Posted in group: nodejs
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

On Apr 27, 3:39 pm, Micheil Smith <mich...@brandedcode.com> wrote:
> I wouldn't be using redis for this, definitely would use either zmq, rabbitmq,
> or thrift. Purely because it's kinda hard to scale redis' pub/sub stuff, and
> can be a SPOF.
>
> – Micheil
>
> On 26/04/2012, at 5:08 PM, Bika wrote:
>
>
>
>
>
>
>
> > I've read about a similar problem here [1], they basically write from node to Redis, and use Redis' pub/sub to broadcast the changes to django.
>
> > [1]:http://lincolnloop.com/blog/2012/apr/23/ginger-tech-stack/
>
> > 2012. április 25., szerda 15:15:35 UTC+2 időpontban Cassio Melo a következőt írta:
> > I'm beginning with websockets and I'm 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.
>
> > I'm not sure which is the best way of doing that (supposing the method
> > to be executed in django takes some time to finish). I see two
> > possibilities:
>
> > - node.js send async http requests to django server
> > - node.js communicates with django using AMQP
>
> > Can you give your view about it?
>
> > --
> > Job Board:http://jobs.nodejs.org/
> > Posting guidelines:https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> > You received this message because you are subscribed to the Google
> > Groups "nodejs" group.
> > To post to this group, send email to nod...@googlegroups.com
> > To unsubscribe from this group, send email to
> > nodejs+un...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/nodejs?hl=en?hl=en