niladalia/rabbit-messenger-bundle

There is no license information available for the latest version (dev-master) of this package.

A simple Symfony bundle that says 'Hello, World!'

dev-master 2024-10-30 11:41 UTC

This package is not auto-updated.

Last update: 2024-11-13 11:48:05 UTC


README

This repository contains a simple template for building event-driven microservices using the Rabbit Messenger Bundle.

Requirements

You need to have a running RabbitMQ configuration or a Docker setup to use this bundle effectively.

Installation

To integrate the Rabbit Messenger Bundle into your project, run the following command:

composer update niladalia/rabbit-messenger-bundle

Usage

Domain Events

Create Domain Events

Your domain event classes should extend the DomainEvent class provided by the bundle:

namespace RabbitMessengerBundle\Domain\Event;

class YourDomainEvent extends DomainEvent {
    // Your event properties and methods
}

Publishing Events

To publish events, inject the DomainEventPublisherInterface into the classes where you need to publish events. Call the publish method as shown below:

use RabbitMessengerBundle\Domain\Event\DomainEventPublisherInterface;

class YourService {
private $eventPublisher;

    public function __construct(DomainEventPublisherInterface $eventPublisher) {
        $this->eventPublisher = $eventPublisher;
    }

    public function someMethod() {
        $event = new YourDomainEvent(/* event data */);
        $this->eventPublisher->publish($event);
    }
}

Configuration

Messenger Configuration

In the messenger.yaml file of both your sender application and listener, specify the serializer as follows:

  serializer: RabbitMessengerBundle\Infrastructure\Serializer\MessengerSerializer

Event Listener Configuration

Implement the Domain Event Mapper

Your listener or receiver micro should implement the DomainEventMapperInterface.

This interface is responsible for mapping event types to the corresponding event classes in your microservice.

Your implementation must return an array with the following keys:

$map = [
    'aggregateId'
    'eventId'
    'attributes'
    'occurredOn'
];

It will also need to return a string with the full path for the domain event class of the recieved event from Rabbit.

Service Configuration

In the services.yaml file, specify the implementation for the DomainEventMapperInterface and ensure it is public:

App\Shared\Infrastructure\Event\DomainEventMapper:
    public: true  # Ensure the mapper is accessible

RabbitMessengerBundle\Domain\Event\Mapper\DomainEventMapperInterface: '@App\Shared\Infrastructure\Event\DomainEventMapper'