Using RabbitMQ with MonsterMQ Part 1

This article is intended for those who are not familiar with queues and RabbitMQ yet. Those who already know how to work with RabbitMQ and only want to explore the possibilities that MonsterMQ provides, I recommend visiting the project page on github.com, where the description describes in detail how you can use MonsterMQ without describing the basics of working with RabbitMQ. The rest of this article will cover the basics of how RabbitMQ works with MonsterMQ.

RabbitMQ is a message broker. It receives, stores and forwards messages to its customers. Clients senders and recipients of messages can serve, for example, applications written in PHP. Communication between the client and RabbitMQ occurs according to certain rules called AMQP (Advanced Message Queuing Protocol). For example, communication between your browser and the web server on which this article is located also follows certain rules, they are called HTTP (HyperText Transfer Protocol) - a hypertext transfer protocol. Both HTTP and AMQP are client-server protocols, that is, they mean the presence of a client and a server. RabbitMQ is an AMQP server, and MonsterMQ is a library that allows you to quickly write an AMQP client in PHP, which will communicate with the AMQP server.

AMQP and RabbitMQ use the following terms:

Producing - means to send messages, Producer (supplier) - the one who sends messages.

image

Consuming - means to receive messages. Consumer - the one who receives messages.

image

Queue is the queue. Before reaching the consumer and after it was sent by the supplier, the message is stored in a queue inside RabbitMQ. The queue is a buffer that stores messages. Many providers can send messages in one queue, and many consumers can read messages from one queue.

image

Note that the provider, consumer and broker do not have to be on the same host, on the contrary they are usually located on different.

Hello world


Later in this article, we will write two programs in PHP using the MonsterMQ library, one of which will send messages, and the other will receive and output them to the console. In the figure below, ā€œPā€ is the provider, ā€œCā€ is the consumer, red boxes are the queue that is the message buffer (each box is the message).

image
(Image taken from the official RabbitMQ website )

For further work, we need to install MonsterMQ, to do this, enter the following command in the console:

composer require goootlib/monster-mq:dev-master

(More about installing MonsterMQ can be found on the project page on github.com)
This command will install MonsterMQ in the current folder.

Departure


We will call our sender send.php , and the recipient receive.php . The sender will join the server and send the message.
To join the server, write the following code in send.php :.

try {
   $producer = \MonsterMQ\Client\Producer();

   $producer->connect('127.0.0.1', 5672);
   $producer->logIn('guest', 'guest');
} catch(\Exception $e) {
   var_dump($e);
}

In this code, we join RabbitMQ running on a local server on a standard port. Specify other ip-address and port if RabbitMQ is running on your other address. After the connection, we also call the logIn () method, which starts the session with the standard login and password. If you have an account with a different username and password configured in RabbitMQ, specify them here.

To send a message, we must declare the queue in which it will first be placed. After the queue is announced, we can send a message:

try {
   $producer = \MonsterMQ\Client\Producer();

   $producer->connect('127.0.0.1', 5672);
   $producer->logIn('guest', 'guest');

   $producer->queue('test-queue')->declare();
   $producer->publish('my-message', 'test-queue');
} catch(\Exception $e) {
   var_dump($e);
}

The queue declaration is idempotent, that is, it will not create and will not change the queue, if it already exists. After we complete the session and join.

try {
   $producer = \MonsterMQ\Client\Producer();

   $producer->connect('127.0.0.1', 5672);
   $producer->logIn('guest', 'guest');

   $producer->queue('test-queue')->declare();
   $producer->publish('Test message', 'test-queue');

   $producer->disconnect();
} catch(\Exception $e) {
   var_dump($e);
}

Receive message


Unlike the sender, the recipient will remain running in order to listen to incoming messages. In the receive.php code, you first need to do the same as in send.php :

try {
   $consumer = \MonsterMQ\Client\Consumer();

   $consumer->connect('127.0.0.1', 5672);
   $consumer->logIn('guest', 'guest');

   $consumer->queue('test-queue')->declare();
} catch(\Exception $e) {
   var_dump($e);
}

Note that in receive.php we also declare our queue, this is so that the recipient declares the queue if it is started before the sender.
In order to tell the server that we want to receive a message from it, as well as to start a cycle that will wait and process incoming messages, we will write the following code:

try {
   $consumer = \MonsterMQ\Client\Consumer();

   $consumer->connect('127.0.0.1', 5672);
   $consumer->logIn('guest', 'guest');

   $consumer->queue('test-queue')->declare();

   $consumer->consume('test-queue');

   $consumer->wait(function ($message, $channelNumber) use ($consumer){
      echo "Message - {$message} received on channel {$channelNumber}";
   });
} catch(\Exception $e) {
   var_dump($e);
}

Launch


Now we can run both scripts as the recipient:

php receive.php

so sender:

php send.php

The recipient will output to the console the message that he will receive from RabbitMQ (which in turn will receive it from send.php) and the channel number used. It will continue to be executed until you close the terminal, or until you stop executing it by pressing Ctrl + C. By launching the sender in another terminal, you can observe how messages are delivered to the recipient and displayed in the console.

If you want to see which, at the moment, RabbitMQ contains queues and how many messages are in them, you can do this as a privileged user using rabbitmqctl
sudo rabbitmqctl list_queues


On windows do not use sudo
rabbitmqctl.bat list_queues


Now we are ready to move on to the next part of our lessons.

All Articles