RabbitMQ is a message broker

Amol Gunjal
3 min readMay 1, 2020

--

A simple example of a rabbitMQ is written in PHP

RabbitMQ accepts and forwards messages.

You can think about it as a post office: when a person X puts the mail that he/she want posting in a post box, he/she can be sure that Mr. or Ms. Mailperson will eventually deliver the mail to his/her recipient. In this analogy, RabbitMQ is a post box, a post office, and a postman.

In short, It accepts, stores, and forwards binary blobs of data ‒ messages.

RabbitMQ, and messaging in general, uses some words.

Producer: A program that sends messages.

Queue: The messages flow through RabbitMQ and your applications, they can only be stored inside a queue.

A queue is only bound by the host’s memory & disk limits, it’s essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue. This is how we represent a queue.

Consumer: It is a program that mostly waits to receive messages

Note: The producer, consumer, and broker do not have to reside on the same host; indeed in most applications they don’t. An application can be both a producer and a consumer, too.

Prerequisites

We assume RabbitMQ is installed and running on the localhost on standard port (5672).

The php-amqplib client library

RabbitMQ speaks multiple protocols. This example covers AMQP 0–9–1, which is an open, general-purpose protocol for messaging. There are a number of clients for RabbitMQ in many different languages. We’ll use the php-amqplib in this example, and Composer for dependency management.

Add a composer.json file to your project:
{
"require": {
"php-amqplib/php-amqplib": ">=2.9.0"
}
}

Sending

We’ll call our message publisher (sender) send.php and our message receiver receive.php. The publisher will connect to RabbitMQ, send a single message, then exit.

In send.php, we need to include the library and use the necessary classes:

require_once __DIR__ . ‘/vendor/autoload.php’;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

then we can create a connection to the server:

$connection = new AMQPStreamConnection(‘localhost’, 5672, ‘guest’, ‘guest’);
$channel = $connection->channel();

To send, we must declare a queue for us to send to; then we can publish a message to the queue:

$channel->queue_declare(‘queuename’, false, false, false, false);
$msg = new AMQPMessage(‘Hey, Amol!’);
$channel->basic_publish($msg, ‘’, ‘queuename’);
echo “ [x] Sent ‘Message’\n”;

Lastly, we close the channel and the connection;

$channel->close();
$connection->close();

That’s it for our publisher

Receiving

Our receiver listening for messages from RabbitMQ, so unlike the publisher which publishes a single message, we’ll keep it running to listen for messages and print them out.

The code (in receive.php) has almost the same include and uses as send:

require_once __DIR__ . ‘/vendor/autoload.php’;
use PhpAmqpLib\Connection\AMQPStreamConnection;

We open a connection & 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(‘queuename’, false, false, false, false);
echo “ [*] Waiting for messages. To exit press CTRL+C\n”;

We’re about to tell the server to deliver us the messages from the queue. We will define a PHP callable that will receive the messages sent by the server. Keep in mind that messages are sent asynchronously from the server to the clients.

$callback = function ($msg) {
echo ‘ [x] Received ‘, $msg->body, “\n”;
};
$channel->basic_consume(‘queuename’, ‘’, false, true, false, false, $callback);
while ($channel->is_consuming()) {
$channel->wait();
}

Our code will block while our $channel has callbacks. Whenever we receive a message our $callback function will be passed the received message.

Putting it all together

Now we can run both scripts. In a terminal, run the consumer (receiver):

php receive.php

then, run the publisher (sender):

php send.php

The consumer will print the message it gets from the sender via RabbitMQ.

The receiver will keep running, waiting for messages (Use Ctrl-C to stop it), so try running the sender from another terminal.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Amol Gunjal
Amol Gunjal

Responses (1)

Write a response