PHPackages                             semako/rabbitmq-service-provider - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. semako/rabbitmq-service-provider

ActiveLibrary[Queues &amp; Workers](/categories/queues)

semako/rabbitmq-service-provider
================================

Silex service provider for RabbitMQ

1.0.2(9y ago)04MITPHPPHP &gt;=5.4.0

Since Sep 5Pushed 9y ago1 watchersCompare

[ Source](https://github.com/semako/rabbitmq-service-provider)[ Packagist](https://packagist.org/packages/semako/rabbitmq-service-provider)[ Docs](http://github.com/fiunchinho/rabbitmq-service-provider)[ RSS](/packages/semako-rabbitmq-service-provider/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

RabbitMq Service Provider for Silex
===================================

[](#rabbitmq-service-provider-for-silex)

About
-----

[](#about)

This Silex service provider incorporates the awesome [RabbitMqBundle](http://github.com/videlalvaro/RabbitMqBundle) into your Silex Application. Installing this bundle created by [Alvaro Videla](https://twitter.com/old_sound) you can use [RabbitMQ](http://www.rabbitmq.com/) messaging features in your application, using the [php-amqplib](http://github.com/videlalvaro/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](https://github.com/videlalvaro/RabbitMqBundle/blob/master/README.md).

Installation
------------

[](#installation)

Require the library with Composer:

```
$ composer require fiunchinho/rabbitmq-service-provider

```

Then, to activate the service, register the service provider after creating your Silex Application:

```
use Silex\Application;
use fiunchinho\Silex\Provider\RabbitServiceProvider;

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

Start sending messages ;)

Usage
-----

[](#usage)

In the [README](https://github.com/videlalvaro/RabbitMqBundle/blob/master/README.md) 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.

Consumers in the command line
-----------------------------

[](#consumers-in-the-command-line)

We recommend you to use the Consumer command to consume messages from the queues. To use this command, just create the executable for console (as in any console applicaiton)

```
#!/usr/bin/env php
