Sending and receiving to/from the MQ

RabbitMQ uses a protocol called Advanced Message Queuing Protocol (AMQP) for communication, which is a standard for all messaging middleware. This means that we can effectively swap out RabbitMQ for a more robust system, such as Kafka, in the future. This also means that, for the most part, all of the concepts we cover here will likely apply to similar messaging systems.

The first thing we will do is put a message on the queue from a very simple Python client. Open up the source file Chapter_4_3.py and follow these steps:

  1. Open the source code file and take a look:
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!') print(" [x] Sent 'Hello World!'") connection.close()
  1. The code is taken from the RabbitMQ reference tutorial and shows how to connect. It first connects to the hub and opens a queue called hello. A queue is like a mailbox or stack of messages. A hub may have several different queues. Then the code publishes a message to the hello queue with the body of Hello World!.
  2. Before we can run the sample, we first need to install Pika. Pika is an AMQP connection library and can be installed with the following command:
pip install pika

  1. Then run the code file as you normally would and watch the output. It's not very exciting, is it?
  2. Go to the RabbitMQ management interface again at http://localhost:15672/ and see that we now have a single message in the hub, as follows:

RabbitMQ interface showing the addition of a message
  1. The message we just sent will stay on the hub until we collect it later. This single feature will allow us to run individual services and make sure they are communicating correctly without having to worry about other consumers or publishers.

For the purposes of RabbitMQ, we just wrote a publisher. In some cases, you many want a service or app to just publish messages, while in others you may want them to consume them. In the next exercise, Chapter_4_4_py, we will write a hub consumer or client:

  1. Open the source file Chapter_4_4.py and look at the code:
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

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

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
  1. The preceding code is almost identical to the previous example, except that this time it only consumes from the queue using an internal callback function to receive the response. In this example, also note how the script blocks itself and waits for the message. In most cases, the client will register a callback with the queue in order to register an event. That event is triggered when a new message enters the particular queue.
  2. Run the code as you normally would and watch the first Hello World message get pulled from the queue and output on the client window.
  3. Keep the client running and run another instance of the Chapter_4_3.py (publish) script and note how the client quickly consumes it and outputs it to the window.

This completes the simple send and receive communication to/from the message hub. As you can see, the code is fairly straightforward and the configuration works out of the box, for the most part. If you do experience any issues with this setup, be sure to consult the RabbitMQ tutorials, which are an additional excellent resource for extra help. In the next section, we look at how to build the working chatbot server example.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset