Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

texthtml/pimple-rabbitmq-provider

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RabbitMq Service Provider for Pimple / Silex 2

About

This Pimple service provider incorporates the awesome RabbitMqBundle into your application. Installing this bundle created by Alvaro Videla you can use RabbitMQ messaging features in your application, using the php-amqplib library.

After installing this service provider, sending messages from a controller would be something like

$app->post('/message', function(Request $request) use ($app){
    $producer = $app['rabbit.producer']['my_exchange_name'];
    $producer->publish('Some message');

    return new Response($msg_body);
});

Later when you want to consume 50 messages out of the queue names 'my_queue', you just run on the CLI:

$ ./app/console rabbitmq:consumer -m 50 my_queue

To learn what you can do with the bundle, please read the bundle's README.

Installation

Require the library with Composer:

$ composer require texthtml/pimple-rabbitmq-provider

Then, to activate the service, register the service provider after creating your Pimple Container. With Silex 2:

use Silex\Application;
use TH\RabbitmqProvider\RabbitServiceProvider;

$app = new Application();
$app->register(new RabbitServiceProvider());

Start sending messages ;)

Usage

In the README file from the Symfony bundle you can see all the available options. For example, to configure our service with two different connections and a couple of producers, and one consumer, we will pass the following configuration:

$app->register(new RabbitServiceProvider(), [
    'rabbit.connections' => [
        'default' => [
            'host'      => 'localhost',
            'port'      => 5672,
            'user'      => 'guest',
            'password'  => 'guest',
            'vhost'     => '/'
        ],
        'another' => [
            'host'      => 'another_host',
            'port'      => 5672,
            'user'      => 'guest',
            'password'  => 'guest',
            'vhost'     => '/'
        ]
    ],
    'rabbit.producers' => [
        'first_producer' => [
            'connection'        => 'another',
            'exchange_options'  => ['name' => 'a_exchange', 'type' => 'topic']
        ],
        'second_producer' => [
            'connection'        => 'default',
            'exchange_options'  => ['name' => 'a_exchange', 'type' => 'topic']
        ],
    ],
    'rabbit.consumers' => [
        'a_consumer' => [
            'connection'        => 'default',
            'exchange_options'  => ['name' => 'a_exchange','type' => 'topic'],
            'queue_options'     => ['name' => 'a_queue', 'routing_keys' => ['foo.#']],
            'callback'          => 'your_consumer_service'
        ]
    ]
]);

Keep in mind that the callback that you choose in the consumer needs to be a service that has been registered in the Pimple container. Consumer services implement the ConsumerInterface, which has a execute() public method.

Credits