postwait / node-amqp

[UNMAINTAINED] node-amqp is an AMQP client for nodejs
MIT License
1.69k stars 358 forks source link

routing_key versus queue #87

Open funston opened 12 years ago

funston commented 12 years ago

I have some python code below that pushes to a topic exchange, with a bound queue and consumers that bind to the queue and using a routing_key to specify which messages.

Struggling to map this up to this npm, as it seems the "routing_key" on the publish is actually the queue name? What I'm doing is:

conn.queue("foobar", {'durable': true,'autoDelete':false}, function(q) { q.bind("a.b.c"); q.on('queueBindOk', function() { conn.publish("a.b.c", "hello"); }); });

However, the consumer/receiver below never receives the message. If i change the conn.publish to have the "queue_name", ie, foobar as the first parameter, it works, and RabbitMQ admin shows a new queue created called foobar.....

Maybe my wires are crossed, but it seems "routing_key" in this implementation (over the pika one) is actually the "queue_name" ???

The python code below works, allowing consumers to get messages specifying particular routing keys off a named queue

Send message with routing key:

!/usr/bin/env python

import pika import sys

ex = "test.exchange" q = "foobar"

connection = pika.BlockingConnection(pika.ConnectionParameters( host='127.0.0.1')) channel = connection.channel() channel.exchange_declare(exchange=ex, type='topic', durable=True) result = channel.queue_declare(queue=q, durable=True) print result.method.queue channel.queue_bind(exchange='test.mogjs', queue=q) routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info' message = "hello" for i in range(1,20): channel.basic_publish(exchange=ex, routing_key=routing_key, body=message, properties=pika.BasicProperties( delivery_mode = 2, )) print " [x] Sent %r" % (message,)

connection.close()

Recv message with routing key:

!/usr/bin/env python

import pika import sys

ex = "test.exchange" q = "foobar"

connection = pika.BlockingConnection(pika.ConnectionParameters( host='127.0.0.1')) channel = connection.channel() channel.exchange_declare(exchange=ex, type='topic', durable=True)

result = channel.queue_declare(queue=q, durable=True) queue_name = result.method.queue binding_keys = sys.argv[1:] if not binding_keys: print >> sys.stderr, "Usage: %s [binding_key]..." % (sys.argv[0],) sys.exit(1)

for binding_key in binding_keys: channel.queue_bind(exchange=ex, queue=queue_name, routing_key=binding_key)

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

def callback(ch, method, properties, body): print "Routing key: %r" % (method.routing_key) print "Body %r" % (body,)

channel.basic_consume(callback, queue=queue_name, no_ack=True)

channel.start_consuming()

funston commented 12 years ago

Running the following, I'm seeing something unexpected. I setup a single exchange, a single queue which binds to two different routing keys. However, if I bind to routing key "FOOBAR" and publish, the 2nd instance which is bound to "BARFOO" sees the "FOOBAR" routing key message delivered.

% node test.js FOOBAR

% node test.js BARFOO

var amqp = require('amqp');

var connection = amqp.createConnection({ host: 'localhost' });

connection.addListener('ready', function(){ var exchange = connection.exchange('some-exchange'); var options = {'durable':true, 'autoDelete':false}; var queue = connection.queue('queueZ', options,function(queue){ queue.subscribe( {ack:true}, function(message){ console.log("got message"); console.log(message); queue.shift() }); var rk = process.argv[2]; console.log("routing key: " + rk); queue.bind('some-exchange', rk); var msg = {msg:"MATCH "+rk}; exchange.publish(process.argv[2],msg); exchange.publish(process.argv[2],msg); msg = {msg:"NO MATCH: key.a.b"}; exchange.publish("key.a.b", msg); exchange.publish("key.a.b", msg);

});

});

seanhess commented 12 years ago

+1 this is happening to me too

funston commented 11 years ago

still happening, is there any example of how to use topic exchanges and routing keys with this npm?