Giter VIP home page Giter VIP logo

rabbitmq-service-provider's Introduction

RabbitMq Service Provider for Silex

About

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

This is a fork of fiunchinho's rabbitmq-service-provider, the original maintainer does not continue maintenance. Original namespaces have been kept for easy upgrading.

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 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

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.

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
<?php

require_once 'vendor/autoload.php';

use Silex\Application;
use fiunchinho\Silex\Provider\RabbitServiceProvider;
use fiunchinho\Silex\Command\Consumer;
use Ivoba\Silex\Provider\ConsoleServiceProvider;

$app = new Application();
require __DIR__.'/config/dev.php';

$app->register(new RabbitServiceProvider(), array(
    'rabbit.consumers' => [
        'my_consumer' => [
            'connection'        => 'default',
            'exchange_options'  => ['name' => 'my_exchange_name','type' => 'topic'],
            'queue_options'     => ['name' => 'a_queue', 'routing_keys' => ['foo.#']],
            'callback'          => 'my_service'
        ]
    ]
));

$app->register(new ConsoleServiceProvider(), array(
    'console.name'              => 'MyApplication',
    'console.version'           => '1.0.0',
    'console.project_directory' => __DIR__
));

$application = $app['console'];
$application->add(new Consumer());
$application->run();

We rely on the Ivoba\Silex\Provider\ConsoleServiceProvider to make things easier, so you have to register it too. You can create new commands by inheriting from the example Consumer, and adding them as the example above.

Credits

rabbitmq-service-provider's People

Contributors

antonstoeckl avatar fiunchinho avatar williamespindola avatar alexstansfield avatar kpacha avatar

Watchers

Ramon Sanches avatar James Cloos 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.