https://github.com/amqp-reply/amqp-reply
Extension of Symfony's amqp-messenger that abstracts RabbitMQ's RPC (Remote Procedure Call) functionality
https://github.com/amqp-reply/amqp-reply
microservices queue rabbitmq rpc symfony
Last synced: 3 months ago
JSON representation
Extension of Symfony's amqp-messenger that abstracts RabbitMQ's RPC (Remote Procedure Call) functionality
- Host: GitHub
- URL: https://github.com/amqp-reply/amqp-reply
- Owner: amqp-reply
- License: mit
- Created: 2025-01-26T10:03:27.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-03-18T13:41:51.000Z (3 months ago)
- Last Synced: 2025-03-18T17:59:47.989Z (3 months ago)
- Topics: microservices, queue, rabbitmq, rpc, symfony
- Language: PHP
- Homepage:
- Size: 23.4 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AMQP Reply Bundle
[](https://codecov.io/gh/conejerock/ampq-bundle)
AMQP Reply Bundle is an extension of Symfony's `amqp-messenger` that abstracts RabbitMQ's RPC (Remote Procedure Call) functionality. It provides a simple and flexible way to send messages and wait for a response in Symfony microservices architecture.
## Why Use AMQP Reply Bundle?
- **Simple RPC Handling:** No need to manually implement RabbitMQ RPC.
- **Seamless Symfony Integration:** Works directly with Symfony Messenger.
- **Improved Microservices Communication:** Enables synchronous message processing while maintaining decoupled services.
- **Reliable Response Management:** Built-in support for handling responses efficiently.## Installation
```sh
composer require amqp-reply/amqp-reply
```## Configuration
Ensure your Symfony Messenger transport configuration supports AMQP:
```yaml
framework:
messenger:
transports:
my_transport_sync:
dsn: 'amqp://guest:guest@rabbitmq:5672'
options:
exchange:
name: query_exchange
type: topic
queues:
query_queue: ~
reply: # Option to enable amqp-messenger
timeout: 5
prefix: 'my_reply_'
routing:
'App\Query\MyQuery': my_transport_sync```
## Usage
### Sending a Request and Waiting for a Reply
```php
declare(strict_types=1);namespace App;
use Symfony\Component\Messenger\MessageBusInterface;
use App\Query\MyQuery;
use App\Query\MyQueryResponse;final class RpcClient
{
public function __construct(private MessageBusInterface $bus) {}public function __invoke(): string
{
$handledStamp = $this->bus->dispatch(new MyQuery($id))->last(HandledStamp::class)
/** @var MyQueryResponse $response */
$response = $handledStamp->getResult();
return $response->message;
}
}
```### Handling the Request in a Consumer
```php
declare(strict_types=1);namespace App\Query;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
final class MyQuery
{
public function __construct(public readonly string $id) {}
}final class MyQueryResponse
{
public function __construct(public readonly string $message) {}
}#[AsMessageHandler]
class MyQueryHandler
{public function __invoke(MyQuery $query): MyQueryResponse
{
$message = 'Hello, ' . $query->id;
return new MyQueryResponse($message);
}
}
```### Consuming requests
```sh
php bin/console messenger:consume my_transport_sync
```## License
This library is licensed under the [MIT License](LICENSE).
## Author
Developed by [Juanjo Conejero](https://juanjoconejero.com).
## Contribute
Contributions are welcome! Feel free to open issues and pull requests to improve this library.