“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() asyncio.get_event_loop().run_until_complete(connect())
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:
We’re now ready to publish message on to this queue:
await channel.basic_publish( payload='Hello World!', exchange_name='', routing_key='hello' )
We can now close the connection to rabbit:
# close using the `AMQP` protocol await protocol.close() # ensure the socket is closed. transport.close()
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.
To consume a message, the library calls a callback (which MUST be a coroutine):
async def callback(channel, body, envelope, properties): print(body) await channel.basic_consume(callback, queue_name='hello', no_ack=True)