Giter VIP home page Giter VIP logo

rabbitmq-adapter's Introduction

RabbitMQ Adapter Package

Description of your RabbitMQ adapter package.

Installation

You can install the package via Composer:

composer require dreanarc/rabbitmq-adapter

Usage/Examples

PHP Native

If you're using PHP Native, follow these steps:

  • Install composer
  • Create PHP Project directory
  • Init PHP project using composer
  • Install the package via composer
    composer require dreanarc/rabbitmq-adapter
  • Code Example for Queue Producer
    require 'vendor/autoload.php';
    
    use Dreanarc\RabbitMQAdapter\RabbitMQProducer;
    
    $producer = new RabbitMQProducer(
        'localhost',
        '5672',
        'guest',
        'guest'
    );
    
    $data = "";
    
    $producer->sendMessage('queue_name', $data);
  • Code Example for Queue Consumer
    require 'vendor/autoload.php';
    
    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;
    });

Codeigniter 3.x

If you're using Codeigniter 3.x, follow these steps:

  • Install the package via composer

    composer require dreanarc/rabbitmq-adapter
  • create config file application\config\rabbitmq.php

    <?php 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';
  • Do not forget add rabbitmq in application\config\autoload.php

    $autoload['config'] = array('rabbitmq');
  • Create custom library application\libraries\Rabbitmq_Adapter.php

    <?php
    
    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;
            }
        }
    }
  • Code Example for Queue adapter

    <?php
    
    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;
            });
        }
    } 

Demo

Insert gif or link to demo

License

MIT

rabbitmq-adapter's People

Contributors

andre-arc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.