Simple library for sending reliable messages among apps

v1.2.1 2019-12-06 14:03 UTC

This package is auto-updated.

Last update: 2020-12-29 12:29:51 UTC


Build Status
License: MIT

Imatic Notification

Application for simplifying using message queues


Library needs 2 configuration options to by specified by you. How to specify them depends on your service container (see Acessing to the services section).

  • imatic_notification_params
    • parameters of the connection to the broker
$params = [
    'host' => 'localhost',
    'port' => 5672,
    'user' => 'guest',
    'password' => 'guest',
    'namespace' => '',
  • imatic_notification.logger
    • logger implementing interface of psr log
    • in our examples below, we did use NullLogger, which will log nothing - we highly discourage from usage if this logger in production since you'll have no Idea what went wrong if something wrong happend

Interface of the library

You will work with 2 interfaces Publisher for publishing messages into queues and Consumer to consume messages published by publisher.

namespace Imatic\Notification;

interface Connection
   public function createPublisher(ChannelParams $params);

   public function createConsumer(ChannelParams $params);

interface Publisher
    public function publish(Message $message, $key = '');

interface Consumer
    public function consume($queueName, $key, callable $callback);

    public function wait();

    public function waitN($n);

These 2 interfaces are implemented by service imatic_notification.connection

Accessing to the services

It is advised to use one container to create connection object for you from definition file "config/services.yml". Below you can see 2 of many possible ways to go.

Using Pimple

To load services for pimple, you need to add yml2pimple dependency into your composer.json

// create instance of container with required parameters
$pimple = new Container([
    'imatic_notification_params' => [],
    'imatic_notification.logger' => new NullLogger(),

// load services using yaml2pimple
$builder = new ContainerBuilder($pimple);
$locator = new FileLocator([
    __DIR__ . '/../vendor/imatic/notification/config',
$loader = new YamlFileLoader($builder, $locator);

// then you can access to the services
$connection = $pimple['imatic_notification.connection'];

Using Symfony

To load services for Symfony, you need to edit your your app/config/config.yml

    - { resource: ../../vendor/imatic/notification/config/services.yml }

    imatic_notification_params: []

        class: Psr\Log\NullLogger

And then you can access to the services from your Symfony container

$connection = $this->container->get('imatic_notification.connection');

Usage example

// create connection to the broker
$connection = $this->container->get('imatic_notification.connection');

// create channel parameters
$channelParams = new ChannelParams($exchange = 'imatic_queue_test');

// create consumer
$consumer = $connection->createConsumer($channelParams);

// listen to the messages on queue "queue_name"
// to all messages having routing key "routing_key"
$consumer->consume('queue_name', 'routing_key', function (Message $msg) {

    // you need to return true to tell the broker that it can discard the messaga
    // because you successfully processed it
    return true;

// create publisher
$publisher = $connection->createPublisher($channelParams);

// publish message to the channel with routing key "routing_key"
$publisher->publish(new Message(['data' => 'bdy']), 'routing_key');

// consume only 1 message, then continue

// won't return till you have listening consumers