Implementing Message Queues with PHP and RabbitMQ

This article explains how to set up a PHP development environment, install RabbitMQ and its dependencies, and provides complete producer and consumer code examples to create a simple "Hello World" message queue using RabbitMQ's AMQP protocol.

php Courses
php Courses
php Courses
Implementing Message Queues with PHP and RabbitMQ

Message queues are an asynchronous communication protocol that enable decoupling and asynchronous processing in distributed architectures; in web development they are commonly used for handling background tasks, load balancing, and event‑driven designs. This guide demonstrates how to develop a message queue using PHP and RabbitMQ.

Environment requirements

PHP 5.4 or higher

RabbitMQ server

AMQP PHP extension (e.g., php-amqplib)

Installing Erlang and RabbitMQ

On CentOS: sudo yum install erlang On Ubuntu: sudo apt-get install erlang Download and extract RabbitMQ:

wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.7/rabbitmq-server-generic-unix-3.7.7.tar.xz
sudo mv rabbitmq_server-3.7.7/ /usr/local/rabbitmq/

Set the PATH and start the server:

export PATH=/usr/local/rabbitmq/sbin:$PATH
rabbitmq-server -detached

Verify that RabbitMQ is running: sudo rabbitmqctl status The command outputs details such as memory limits, running applications, listeners, and VM arguments, confirming the broker is operational.

Producer code (producer.php)

<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

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

$channel->queue_declare('hello', false, false, false, false);

$message = new AMQPMessage('Hello World!');
$channel->basic_publish($message, '', 'hello');

echo " [x] Sent 'Hello World!'
";

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

Run the producer: php producer.php Expected output: [x] Sent 'Hello World!' Consumer code (consumer.php)

<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

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

$channel->queue_declare('hello', false, false, false, false);

echo " [*] Waiting for messages. To exit press CTRL+C
";

$callback = function ($msg) {
    echo " [x] Received ", $msg->body, "
";
};

$channel->basic_consume('hello', '', false, true, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}

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

Run the consumer: php consumer.php Expected output: [*] Waiting for messages. To exit press CTRL+C The producer sends a message to the "hello" queue, and the consumer receives and prints it. RabbitMQ uses a push‑based delivery model, so without a consumer the message would remain in the queue.

Conclusion

The article covered the basic concepts of RabbitMQ, detailed installation steps, and provided complete PHP examples for both a producer and a consumer, illustrating how a simple "Hello World" message queue can be built and extended for more complex applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendAsynchronousMessage QueueRabbitMQAMQP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.