Event Bus Queue library. Allows implementing asynchronous Event Bus.

1.1.3 2017-04-16 17:52 UTC

This package is not auto-updated.

Last update: 2024-06-22 17:35:19 UTC


EventBus Queue

Build Status Scrutinizer Code Quality SensioLabsInsight Latest Stable Version Total Downloads License Donate

This package is an extension library for nilportugues/messagebus, adding queues to the EventBus implementation.

This package will provide you for the classes necessary to build:

  • The Producer: code that sends the Event to a Queue by serializing the Event. This happens synchronously.
  • The Consumer: code that reads in the background, therefore asynchronously, reads and unserializes the Event from the Queue and passes it to the EventBus to do the heavy lifting.


It's all about deciding which command logic can be delayed, or hidden away in order to make it faster. And this is what we want to do.

You never remove a bottleneck, you just move it. The downside is that we might have to assume a possible delay.


In order to start using this project you require to install it using Composer:

composer require nilportugues/eventbus-queue


This package will provide you with a new middleware: ProducerEventBusMiddleware.

This middleware requires a serializer and a storage that will depend on the Queue Adapter used. Supported adapters are:

  • PDOQueue: queue built with a SQL database using Doctrine's DBAL.
  • MongoDBQueue: queue built with MongoDB library.
  • RedisQueue: queue using the Redis PHP extension.
  • PredisQueue: queue using the Predis library.
  • FileSystemQueue: queue built with using the local file system.
  • AmqpQueue: use RabbitMQ or any queue implementing the Amqp protocol.
  • BeanstalkdQueue: use Beanstalk as queue.

To set it up, register the ProducerEventBusMiddleware to the Event Bus. Because we'll need to define a second EventBus (consumer), we'll call this the ProducerEventBus.


$container['LoggerEventBusMiddleware'] = function() use ($container) {
    return new \NilPortugues\MessageBus\EventBus\LoggerEventBusMiddleware(

//Definition of the Serializer
$container['NativeSerializer'] = function() use ($container) {
    return new \NilPortugues\MessageBus\Serializer\NativeSerializer();

//Definition of the Queue driver
$container['RabbitMQ'] = function() use ($container) {
    return new AMQPStreamConnection('', 5672, 'guest', 'guest');

//Definition of the Event Bus Queue. For instance RabbitMQ.
$container['EventBusQueueAdapter'] = function() use ($container) {
    return new \NilPortugues\MessageBus\EventBusQueue\Adapters\AmqpQueue(
        'myEventBusQueue' //queue Name

//Definition of the Producer.
$container['ProducerEventBusMiddleware'] = function() use ($container) {
    return new \NilPortugues\MessageBus\EventBusQueue\ProducerEventBusMiddleware(

//This is our ProducerEventBus. 
$container['ProducerEventBus'] = function() use ($container) {
    return new \NilPortugues\MessageBus\EventBus\EventBus([

Consumer for the ProducerEventBus

The Consumer will need to be a script that reads the EventBus definitions and subscribed events in order to run until all events are handled. To do so, we'll need to register a new EventBus we'll refer as ConsumerEventBus.

We will also like to store events that could not be handled or raised an exception. So a new Queue will be required. For instance, let's store errors in a MongoDB database.

This could be as simple as follows:

//This is our ConsumerEventBus. 
$container['ConsumerEventBus'] = function() use ($container) {
    return new \NilPortugues\MessageBus\EventBus\EventBus([

$container['MongoDB'] = function() use ($container) {
    return new \MongoDB\Client();

//This is an error Queue.
$container['ErrorQueue'] = function() use ($container) {
    return new \NilPortugues\MessageBus\EventBusQueue\Adapters\MongoQueue(


Finally, we'll have to call a consumer. This package already provides a fully working consumer implementation: EventBusWorker.

Use it as follows:

//... your $container should be available here.

$consumer = \NilPortugues\MessageBus\EventBusQueue\EventBusWorker();

Consumer class will run the consume method until all events are consumed. Then it will exit. This is optimal to make sure it will not leak memory.

If you need to keep the consumer running forever use server scripts like Supervisor.

Supervisor Configuration

Supervisor is a process monitor for the Linux operating system, and will automatically restart your workers if they fail. To install Supervisor on Ubuntu, you may use the following command:

sudo apt-get install supervisor

Supervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct how your processes should be monitored.

For instance, let's create /etc/supervisor/conf.d/my_worker.conf so that it starts and monitors a worker script named my_worker.php:

command=php my_worker.php

In this file, we tell Supervisor that we want 20 instances always running. If the my_worker.php ends or fails it will spin up a new one.

In order to make this task run forever, you'll have to type in the following commands:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start my_worker

Adapter Configurations


For this to work, you'll be required to create a table in your database.

For instance, sqlite dialect table creation would be:

CREATE TABLE testAdapterQueue (
  event_data TEXT NOT NULL,
  event_status CHAR(50),
  created_at INTEGER NOT NULL


In order to use it, you require to install PHP 7's mongodb extension.

sudo pecl install mongodb


In order to use it, you require to install PHP 7's phpredis extension.

# Build Redis PHP module
git clone -b php7
sudo mv phpredis/ /etc/ && \
cd /etc/phpredis \
phpize \
./configure \
make && make install \
touch /etc/php/7.0/mods-available/redis.ini \
echo '' > /etc/php/7.0/mods-available/redis.ini


Nothing, but performs better if phpredis extension is found.


Nothing to do.


Nothing to do other than having access to a amqp server.


Nothing to do other than having access to a beanstalkd server.


Contributions to the package are always welcome!


Get in touch with me using one of the following means:



The code base is licensed under the MIT license.