To implement pub-sub with RabbitMQ in Symfony, you may follow these steps:

 

Set up RabbitMQ:

Install RabbitMQ on your system or use a hosted RabbitMQ service.
Make sure you have the necessary credentials (username, password) to connect to RabbitMQ.


Install RabbitMQ Bundle:

In your Symfony project, use the RabbitMQ Bundle to integrate RabbitMQ easily. Install the bundle using Composer:

composer require enqueue/amqp-bundle

 

Configuration:
Configure the RabbitMQ connection in the config/packages/enqueue.yaml file:

enqueue:
    transport:
        default: 'amqp://guest:guest@localhost/%2f' # Replace with your RabbitMQ connection details

Create a Publisher:
Create a service that acts as the publisher and publishes messages to RabbitMQ.

// src/Message/PubSubMessage.php
namespace App\Message;

class PubSubMessage
{
    private $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

    public function getMessage(): string
    {
        return $this->message;
    }
}
// src/Message/Publisher.php
namespace App\Message;

use Enqueue\Client\ProducerInterface;

class Publisher
{
    private $producer;

    public function __construct(ProducerInterface $producer)
    {
        $this->producer = $producer;
    }

    public function publish(string $message): void
    {
        $pubSubMessage = new PubSubMessage($message);
        $this->producer->sendEvent('pub_sub_topic', $pubSubMessage);
    }
}

 

Create a Subscriber:
Create a service that acts as the message subscriber to receive messages from RabbitMQ.

// src/Message/Subscriber.php
namespace App\Message;

use Enqueue\Client\TopicSubscriberInterface;
use Interop\Queue\Context;
use Interop\Queue\Message;
use Interop\Queue\Processor;

class Subscriber implements Processor, TopicSubscriberInterface
{
    public static function getSubscribedTopics()
    {
        return ['pub_sub_topic'];
    }

    public function process(Message $message, Context $context)
    {
        // Handle the received message here
        $payload = json_decode($message->getBody(), true);
        // Do something with $payload...
        
        return self::ACK;
    }
}

 

Register the Subscriber as a service in services.yaml:

services:
    App\Message\Subscriber:
        tags:
            - { name: 'enqueue.client.processor', processorName: 'subscriber_processor' }

 

Publish Messages:
You can now publish messages using the Publisher service from your Symfony controllers or services:

use App\Message\Publisher;
// ...

class SomeController extends AbstractController
{
    public function someAction(Publisher $publisher)
    {
        $message = 'This is a pub-sub message.';
        $publisher->publish($message);

        // ...
    }
}

With this setup, when you call the publish method on the Publisher service, it will publish the message to the RabbitMQ exchange under the specified topic ('pub_sub_topic'). The Subscriber will then receive these messages and process them accordingly.

Please note that this example assumes you have RabbitMQ running on 'localhost' with the default credentials (guest/guest). Adjust the connection details in the enqueue.yaml file as per your RabbitMQ setup. Additionally, make sure to configure the RabbitMQ server and exchange correctly to enable pub-sub behavior.