“Hello World!” : The simplest thing that does something


Our first script to send a single message to the queue.

Creating a new connection:

import asyncio
import aioamqp

def connect():
    transport, protocol = yield from aioamqp.connect()
    channel = yield from protocol.channel()


This first scripts shows how to create a new connection to the AMQP broker.

Now we have to declare a new queue to receive our messages:

yield from channel.queue_declare(queue_name='hello')

We’re now ready to publish message on to this queue:

yield from channel.basic_publish(
    payload='Hello World!',

We can now close the connection to rabbit:

# close using the `AMQP` protocol
yield from protocol.close()
# ensure the socket is closed.

You can see the full example in the file example/send.py.


We now want to unqueue the message in the consumer side.

We have to ensure the queue is created. Queue declaration is indempotant.

yield from channel.queue_declare(queue_name='hello')

To consume a message, the library calls a callback (which MUST be a coroutine):

def callback(channel, body, envelope, properties):

yield from channel.basic_consume(callback, queue_name='hello', no_ack=True)