PHP code example of dreanarc / rabbitmq-adapter

1. Go to this page and download the library: Download dreanarc/rabbitmq-adapter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dreanarc / rabbitmq-adapter example snippets


    use Dreanarc\RabbitMQAdapter\RabbitMQProducer;

  $producer = new RabbitMQProducer(
      'localhost',
      '5672',
      'guest',
      'guest'
  );

  $data = "";

  $producer->sendMessage('queue_name', $data);
  

    use Dreanarc\RabbitMQAdapter\RabbitMQConsumer;
  use PhpAmqpLib\Message\AMQPMessage;

  $consumer = new RabbitMQConsumer(
      'localhost',
      '5672',
      'guest',
      'guest'
  );

  $consumer->consume('queue_name', function(AMQPMessage $message){
      echo 'Received message: ', $message->getBody(), PHP_EOL;
  });
  

     defined('BASEPATH') or exit('No direct script access allowed');

    $config['rabbitmq_host'] = 'localhost';
    $config['rabbitmq_port'] = 5672;
    $config['rabbitmq_user'] = 'guest';
    $config['rabbitmq_password'] = 'guest';
    

    $autoload['config'] = array('rabbitmq');
    

    

    use Dreanarc\RabbitMQAdapter\RabbitMQConsumer;
    use Dreanarc\RabbitMQAdapter\RabbitMQProducer;

    class Rabbitmq_Adapter
    {
        private $config;
        function __construct()
        {
            // Load CodeIgniter config
            $CI =& get_instance();

            $this->config['host'] = $CI->config->item('rabbitmq_host') ?: 'localhost';
            $this->config['port'] = $CI->config->item('rabbitmq_port') ?: 5672;
            $this->config['user']= $CI->config->item('rabbitmq_user') ?: 'guest';
            $this->config['password']= $CI->config->item('rabbitmq_password') ?: 'guest';
        }

        function publish_queue($queue_name, $msg){
            $producer = new RabbitMQProducer(
                $this->config['host'],
                $this->config['port'],
                $this->config['user'],
                $this->config['password']
            );

            try {
                $producer->sendMessage($queue_name, $msg);
                return true;
            } catch (Error $rr) {
                return false;
            }
        }

        function consume_queue($queue_name, callable $callback){
            $consumer = new RabbitMQConsumer(
                $this->config['host'],
                $this->config['port'],
                $this->config['user'],
                $this->config['password']
            );

            try {
                $consumer->consume($queue_name, $callback);
            } catch (Error $rr) {
                return false;
            }
        }
    }
    

    

    use PhpAmqpLib\Message\AMQPMessage;

    defined('BASEPATH') OR exit('No direct script access allowed');

    class Queue extends CI_Controller {
        function __construct()
        {
            parent::__construct();
            $this->load->library('Rabbitmq_Adapter', null, 'queue');
        }

        public function publish()
        {
            $this->queue->publish_queue('email.notification', 'tes queue');
        }

        public function consumer()
        {
            $this->queue->consume_queue('email.notification', function(AMQPMessage $message){
                echo 'Received message: ', $message->getBody(), PHP_EOL;
            });
        }
    }