Click here to Skip to main content
15,887,135 members
Articles / Programming Languages / PHP

CakePHP Plugin for Usage of RabbitMQ

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Aug 2023CPOL2 min read 2.4K  
Example of how to use CakePHP with RabbitMQ
CakePHP allows building web applications that interact with RabbitMQ using the PhpAmqpLib library for sending and receiving messages through a configured RabbitMQ connection.

CakePHP is a PHP web application framework that can be used to build web applications that interact with RabbitMQ. Here's an example of how to use CakePHP with RabbitMQ.

1. Install the PHP AMQP Extension

The PHP AMQP extension provides a PHP interface for interacting with RabbitMQ. You can install this extension using PECL by running the following command:

pecl install amqp

If you get error "Zend Extension Api No: 320190902 Cannot find autoconf"

Image 1

Run more command.

brew install autoconf
brew install rabbitmq-c

Composer requires php-amqplib/php-amqplib.

2. Configure the RabbitMQ Connection

In your CakePHP application, you can configure the RabbitMQ connection in the app/config/bootstrap.php file. Here's an example:

PHP
use Cake\Core\Configure;
use PhpAmqpLib\Connection\AMQPStreamConnection;

Configure::write('RabbitMQ', [
    'host' => 'localhost',
    'port' => 5672,
    'username' => 'guest',
    'password' => 'guest',
    'vhost' => '/',
]);

$connection = new AMQPStreamConnection(
    Configure::read('RabbitMQ.host'),
    Configure::read('RabbitMQ.port'),
    Configure::read('RabbitMQ.username'),
    Configure::read('RabbitMQ.password'),
    Configure::read('RabbitMQ.vhost')
);

This code sets the RabbitMQ connection parameters in the Configure object and creates a new AMQPStreamConnection object.

3. Send Messages to RabbitMQ

To send messages to RabbitMQ, you can use the PhpAmqpLib\Channel\AMQPChannel class. Here's an example:

PHP
use PhpAmqpLib\Message\AMQPMessage;

$channel = $connection->channel();

$exchangeName = 'my-exchange';
$queueName = 'my-queue';

$channel->exchange_declare($exchangeName, 'direct', false, true, false);
$channel->queue_declare($queueName, false, true, false, false);
$channel->queue_bind($queueName, $exchangeName);

$messageBody = 'Hello, RabbitMQ!';
$message = new AMQPMessage($messageBody);

$channel->basic_publish($message, $exchangeName);

$channel->close();

This code declares an exchange and a queue, binds the queue to the exchange, creates a new message, and publishes the message to the exchange.

4. Receive Messages from RabbitMQ

To receive messages from RabbitMQ, you can use the PhpAmqpLib\Channel\AMQPChannel class and define a callback function to handle incoming messages. Here's an example:

PHP
use PhpAmqpLib\Message\AMQPMessage; 
$callback = function ($message) {
    echo 'Received message: ' . $message->body . PHP_EOL;
};

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

while (count($channel->callbacks)) {
    $channel->wait();
}

This code sets up a callback function that simply echoes the message body to the console. It then sets up a consumer on the queue and waits for incoming messages.

Overall, using CakePHP with RabbitMQ involves configuring the RabbitMQ connection and using the PhpAmqpLib library to send and receive messages. There are many other features and configurations available in RabbitMQ, and the CakePHP framework has many other features and capabilities as well.

Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about CakePHP plugin for usage of RabbitMQ, please share them in the comments below. I would love to hear from you and discuss this topic further.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Vietnam Vietnam
---------------------------------------------------
Thanks for code project, I joined code project's community in the begin of the year 2008. Code project helped me so much when I still a student.

Now I had working about 14 years (2023). Let Code project plentiful as thank its. So I decide sharing my little knowledge for help another students and developers.

Here is my technical blog: https://learn-tech-tips.blogspot.com
I will share many knowledge about Application, Web, Game, Programming languages C#, C++, Web(HTML, CSS, javascript). Office (Excel, Photoshop) and another useful things Smile | :)

---------------------------------------------------
I'm developer, I like code,
I like to learn new technology and want to be friend with people to improve my skills Smile | :)

Thanks and Best Regards!
Zidane (huuvi168@gmail.com)
https://learn-tech-tips.blogspot.com

Comments and Discussions

 
-- There are no messages in this forum --