CodeIgniter Library for RabbitMQ interactions with CodeIgniter using PHP-AMQPLib
- Rabbit MQ Installed on your server (at least 3.5.*)
- php-amqplib
- CodeIgniter Framework (3.0.* recommanded)
- PHP 5.4+ (with Composer)
Just by running following command in the folder of your project :
composer require romainrg/codeigniter-rabbitmq-libraryOr by adding following lines to your composer.json file :
"require": {
    "romainrg/codeigniter-rabbitmq-library": "^4.0.0"
},Don't forget to include your autoload to CI config file :
$config['composer_autoload'] = FCPATH.'vendor/autoload.php';$ composer require romainrg/codeigniter-rabbitmq-libraryYou have to create it in the CI config folder located in ./application/config/rabbitmq.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Config for Rabbit MQ Library
 */
$config['rabbitmq'] = array(
    'host' => 'localhost',    // <- Your Host     (default: localhost)
    'port' => 5672,           // <- Your Port     (default: 5672)
    'user' => 'username',     // <- Your User     (default: guest)
    'pass' => 'password',     // <- Your Password (default: guest)
    'vhost' => '/'            // <- Your Vhost    (default: /)
);(Or just in a CI Controller)
$this->load->add_package_path(APPPATH . 'third_party/rabbitmq');
$this->load->library('rabbitmq');
$this->load->remove_package_path(APPPATH . 'third_party/rabbitmq');This will create, if it does not exist, the 'hello_queue' queue and insert 'Hello World !' text inside it.
$this->rabbitmq->push('hello_queue', 'Hello World !');If you want to run your CI Controller Method with CLI command :
$ php www.mywebsite.com/index.php 'controller' 'method'You will have the following return
$ [+] Pushing 'Hello World !' to 'hello_queue' -> OKThis will fetch last inserted datas from the 'hello_queue' in real time, with parmanent mode activated and '_process' callback function.
The PHP Code :
return $this->rabbitmq->pull('hello_queue', true, array($this, '_process'));Run it in CLI :
$ php www.mywebsite.com/index.php 'controller' 'method'This will create, if it does not exist, the 'hello_queue' queue and insert 'Hello World !' text inside it, the third parameter TRUE set the durability of the 'hello_queue' (TRUE = permanent, FALSE = not permanent), the last parameter 'delivery_mode (2)' makes message persistent (you can also add some parameters to this array).
$this->rabbitmq->push('hello_queue', 'Hello World !', TRUE, array('delivery_mode' => 2));