Integrating CodeIgniter4 with RabbitMQ Emman, October 2, 2023October 2, 2023 CodeIgniter4 is a PHP web application framework, while RabbitMQ is a message broker that allows you to send and receive messages between different applications or components. Integrating CodeIgniter4 with RabbitMQ involves setting up a messaging system for communication. Below, is a basic example of how you might achieve this integration. Please note that this example assumes you have both CodeIgniter4 and RabbitMQ installed and configured. Step 1: Install Required LibrariesIn your CodeIgniter4 project, you’ll need to install the php-amqplib/php-amqplib library, which provides a PHP implementation of the AMQP protocol used by RabbitMQ. You can use Composer to install it: composer require php-amqplib/php-amqplib Step 2: ConfigurationIn your CodeIgniter4 project, open the app/Config/RabbitMQ.php configuration file (you might need to create it) and add the following configuration settings: <?php namespace Config; use PhpAmqpLib\Connection\AMQPStreamConnection; class RabbitMQ { public $connection; public function __construct() { $this->connection = new AMQPStreamConnection( 'localhost', // RabbitMQ server hostname 5672, // RabbitMQ server port 'guest', // RabbitMQ username 'guest' // RabbitMQ password ); } } Step 3: Sending MessagesNow, let’s create a controller method in CodeIgniter that sends a message to RabbitMQ: <?php namespace App\Controllers; use App\Config\RabbitMQ; use CodeIgniter\Controller; class RabbitMQController extends Controller { public function sendMessage() { $rabbitMQConfig = new RabbitMQ(); $connection = $rabbitMQConfig->connection; $channel = $connection->channel(); $channel->queue_declare('hello', false, false, false, false); $message = 'Hello, RabbitMQ!'; $channel->basic_publish(new \PhpAmqpLib\Message\AMQPMessage($message), '', 'hello'); $channel->close(); $connection->close(); return 'Message sent to RabbitMQ.'; } } Step 4: Receiving MessagesLet’s create another controller method that receives messages from RabbitMQ: <?php namespace App\Controllers; use App\Config\RabbitMQ; use CodeIgniter\Controller; class RabbitMQController extends Controller { public function receiveMessage() { $rabbitMQConfig = new RabbitMQ(); $connection = $rabbitMQConfig->connection; $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 "Received: ", $msg->body, "\n"; }; $channel->basic_consume('hello', '', false, true, false, false, $callback); while ($channel->is_consuming()) { $channel->wait(); } } } Step 5: Accessing the Controller MethodsYou can access the sendMessage and receiveMessage methods by navigating to the respective URLs in your web browser. To send a message, visit: http://your_domain/rabbitmq/sendMessage To receive messages, visit: http://your_domain/rabbitmq/receiveMessage Please note that this example is a basic introduction to integrating CodeIgniter4 with RabbitMQ. In a production environment, you’d likely want to handle exceptions, implement better error handling, and perhaps encapsulate the RabbitMQ functionality into a separate service class for better code organization. Share this:FacebookX Related Discover more from Code Concepts Snippets Subscribe to get the latest posts sent to your email. Type your email… Subscribe General