All in the title : in the methods below from rabbitmq's documentation, we see that publish takes exchange as an argument, but a consumer doesnt.
Also while I am at it, queue
in consumer is the same as routing_key
in the publish ? I thought that a routing key was like a tag so that subscribers subscribe to various regex of tags
code to consume:
import pika
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_consume(callback, queue='hello', no_ack=True)
channel.start_consuming()
code to post:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
connection.close()
You are getting confused about how things work in RabbitMQ. In RabbitMQ you always publish messages to an Exchange
. An exchange is not different from a logical receiver that will route your message to a Queue
(or set of queues).
This routing happens differently depending on the type of the Exchange
. There are different types: Direct, Fanout, Topic and Headers. The way you link an Exchange
to a Queue
is using a Binding
. A binding is defined by a routing key or some headers and it route your messages from a particular exchange to a particular queue.
So, to sum up, you publish messages to an Exchange
. You consume messages from a Queue
. And you use a Binding
to route the messages from the Exchange
to the Queue
. That said, there is no way to say a consumer to consume from an Exchange
.
In your particular case, you are using the routing key 'hello' in your message. That means that you need a Binding
from your Exchange
called '' to the Queue
called 'hello' with the same routing key as your message (which is 'hello'). I would recommend you changing names and having more useful names to see what's going on.
Hope this helps!