You can think of RabbitMQ as a mediator between the two parties. One is known as the producer that produces goods and services and stores them into RabbitMQ. Another one is the consumer that consumes goods and services from the RabbitMQ.
So, RabbitMQ accepts, stores, and forwards binary blobs of data-messages.
The producer is just a program that is used to send messages.
As we know messages are stored and consumed with the help of rabbitMQ but there is a place in the rabbitmq where these messages are stored and this place is known as Queue.
A queue is bound by the host memory & disk limits.
It is a large message buffer that stores messages from multiple producers and multiple consumers trying to consume the data from the same queue.
The consumer is a program that mostly waits to consume messages from the queue.
And it’s not mandatory that producer, consumer, and the queue buffer reside on the same host.
In the above diagram, P is the producer and C is the consumer. And the box that connects them is known as the queue buffer in which the producer stores the messages and the consumer consumes the data from the buffer.
For messaging AMQP 0-9-1 is an open general-purpose protocol.
There are a number of clients for RabbitMQ in many different languages.
We’ll use the php-amqplib and Composer for dependency management.
Steps:
Add a composer.json file to your project:
{
"require": {
"php-amqplib/php-amqplib": ">=2.9.0"
}
}
When the php-amqplib library is installed on your project then you can proceed with the code.
Sending
Here we call our message producer or sender as (producer.php) and message consumer or receiver as (receiver.php).
The producer will publish a message, connected with the RabbitMQ, and send the message and exit.
In producer.php, we have to include library and necessary classes as shown below:
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
And after this connection has to be created with the server.
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
after this we have to create a channel:
$channel = $connection->channel();
Then declare a queue:
$channel->queue_declare(blog, false, false, false, false);
After this, we have to publish a message to this queue.
$msg = new AMQPMessage('RabbitMQ blog!');
$channel->basic_publish($msg, '', 'blog');
echo " [x] Sent 'RabbitMQ blog!'\n";
And then close the channel and the connection
$channel->close();
$connection->close();
Receiving
As we know publisher sends one message to the queue and exits. But the consumer has to run all the time to consume messages and print them.
In reciever.php, we have to include library and necessary classes as shown below:
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
we open a connection and a channel, and declare the queue from which we’re going to consume.
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare(‘blog’, false, false, false, false);
echo " [*] Waiting for messages. To exit press CTRL+C\n";
In consumer.php also declared a blog queue again because sometimes it happens that the consumer starts before the producer so there must be a queue.
$callback = function ($msg) {
echo ' [x] Received ', $msg->body, "\n";
};
$channel->basic_consume(‘blog’, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
$channel->wait();
}
There is a php callable define that helps us to receive the message sent by the queue.
Our code will block while our $channel has callbacks. Whenever we receive a message our $callback function will be passed the received message.
Now we can run both programs producer.php and consumer.php.
In a terminal run:
Run the consumer.php
php consumer.php
After this run the producer.php
php producer.php
The consumer will print the messages that it receives from the buffer and keep running to listen for the messages.