Skip to content

Commit

Permalink
[TASK] Add very basic usage example for TYPO3
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
ochorocho committed Dec 28, 2023
1 parent bb0803c commit 7426d9a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ For more information about the HTTP API see the [official documentation](https:/

You can access the RabbitMQ service through its AMQP protocol inside any DDEV container via `amqp://rabbitmq:5672`

## Examples:

* [TYPO3](USAGE.md)


**Originally Contributed by [@Graloth](https://github.com/Graloth) in [ddev-contrib](https://github.com/ddev/ddev-contrib/tree/master/docker-compose-services/rabbitmq)**

**Maintained by [@b13](https://github.com/b13)**
82 changes: 82 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# TYPO3 and RabbitMQ

This is just a simple example on how RabbitMQ can be used
along with TYPO3.

Install required package:

```bash
ddev composer req symfony/amqp-messenger
```

Create a message with all information needed later on in the handler:

```php
final class MyMessage
{
public function __construct(
public readonly array $content
) {
}
}
```

Process a message:

```php
final class MyHandler
{
public function __construct(private readonly MessageBusInterface $bus)
{
}

public function __invoke(MyMessage $message): void
{
try {
// #### Do magic stuff with $message->content ####
} catch (\Exception $exception) {
// Workaround to support infinite retryable messages. So no message gets lost.
$envelope = new Envelope(new MyMessage($message->content), [new DelayStamp(5000)]);
$this->bus->dispatch($envelope);
}
}
}
```

Register 'amqp' in ext_localconf.php

```php
// Unset the default, so that it no longer applies
unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['messenger']['routing']['*']);
// Set Webhook-Messages and MyMessage to asynchronous transport via amqp
foreach ([WebhookMessageInterface::class, MyMessage::class] as $className) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['messenger']['routing'][$className] = 'amqp';
}
```

Dispatch a message:

```php
class RunTheRabbit implements MiddlewareInterface
{
public function __construct(private readonly MessageBusInterface $bus)
{
}

public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$value = ['some' => 'value'];
$this->bus->dispatch(new MyMessage($value));

return $handler->handle($request);
}
}
```

Start the worker to consume messages:

```bash
ddev typo3 messenger:consume -vv amqp
```

0 comments on commit 7426d9a

Please sign in to comment.