“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

async def connect():
    transport, protocol = await aioamqp.connect()
    channel = await 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:

await channel.queue_declare(queue_name='hello')

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

await channel.basic_publish(
    payload='Hello World!',

We can now close the connection to rabbit:

# close using the `AMQP` protocol
await 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.

await channel.queue_declare(queue_name='hello')

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

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

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