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.
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:
<code>sudo yum install erlang</code>On Ubuntu:
<code>sudo apt-get install erlang</code>Download and extract RabbitMQ:
<code>wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.7/rabbitmq-server-generic-unix-3.7.7.tar.xz</code> <code>sudo mv rabbitmq_server-3.7.7/ /usr/local/rabbitmq/</code>Set the PATH and start the server:
<code>export PATH=/usr/local/rabbitmq/sbin:$PATH
rabbitmq-server -detached</code>Verify that RabbitMQ is running:
<code>sudo rabbitmqctl status</code>The command outputs details such as memory limits, running applications, listeners, and VM arguments, confirming the broker is operational.
Producer code (producer.php)
<code><?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!'\n";
$channel->close();
$connection->close();
</code>Run the producer:
<code>php producer.php</code>Expected output:
<code>[x] Sent 'Hello World!'</code>Consumer code (consumer.php)
<code><?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\n";
$callback = function ($msg) {
echo " [x] Received ", $msg->body, "\n";
};
$channel->basic_consume('hello', '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
$channel->wait();
}
$channel->close();
$connection->close();
</code>Run the consumer:
<code>php consumer.php</code>Expected output:
<code>[*] Waiting for messages. To exit press CTRL+C</code>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.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.