RabbitMQ

RabbitMQ is a server which implements AMQP (Advanced Message Queuing Protocol). It’s general purpose is to help with storing the data for later processing.

Thanks to the fact we can significantly lower the response time for our system (i.e. website) and present the results after the request have been carried out.

RabbitMQ installation.

We’ll start with rabbitmq server and amqp library installation on debian server. After this step we’ll have server running in the backgroup and php available (but it may be necessary to restart web server serivce – i.e. service apache2 restart).

RabbitMQ test.

In Debian Linux distribution, package php-amqplib brings usefull example (available in /usr/share/doc/php-amqplib/examples/)

We’ll use one of the exapmle files and move it to /var/www/html,  so it will be server by the apache2 server as the default site when we access our server (http://ourIPaddress/).

We’ll take the following steps:

  • copy all the files from /usr/share/doc/php-amqplib/examples/ to /var/www/html/amqp
  • change the ownership of the directory and files in var/www/html/amqp to www-data user and www-data group (in the example below we work on root account, which is not best, but fair enough for education purposes on our local virtual machine)

php-amqplib library requires adding some dependencies by using Composer tool.

We’ll take the following steps to accomplish the task:

  • install curl i git tools
  • create the composer.json file
  • we’ll run the composer (downloading it with curl command and piping the output of curl to php interpreter). This step will create composer.phar file.
  • finally we run composer.phar with install option, which will install dependencies.

Note:

  • we took the steps in /var/www/html as current working directory. All the files located in /var/www/html/amqp will refer to this catalogue and look for vendor subdirectory  (available after fulfilling first three steps)

Queue handling .

In /var/www/html/amqp/ directory we place the example file for different queue handling.

We’ll present how to run one of this files, but we first need to modify configuration.

In config.php, we change AMQP_DEBUG to false (so there’ll be no debugging information and the results of our script will be human readable. Of course if we encounter any problem with our script or the library we may change it to true):

Let’s check the content of basic_get.php (presented below, but with all comments removed).  This script first sends ‘test message’ to a queue, and the retrieves it:

 

ToDo:

  • Copy the basic_get.php to two separate files put1.php i get2.php.
  • Modify put1.php so it will only add a message to a queue (removing the retrieving part)
  • Modify get1.php so it will only  fetch the message from a queu (removing the adding part)