Skip to content

Commit 4cbc4ca

Browse files
committed
[Notify] Using Chatter in the docs to show how to use MercureOptions to pass topics
1 parent 2da6586 commit 4cbc4ca

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

src/Notify/doc/index.rst

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,46 +40,46 @@ properly configured notifier transport:
4040
chatter_transports:
4141
myMercureChatter: '%env(MERCURE_DSN)%'
4242
43-
.. note::
44-
45-
It is possible to specify the topics to send the notification in the ``MERCURE_DSN``
46-
environment variable by specifying the ``topics`` query parameter.
47-
Otherwise, notifications will be sent to ``https://symfony.com/notifier`` topic.
48-
4943
Then, you can inject the ``NotifierInterface`` service and send messages on the ``chat/myMercureChatter`` channel::
5044

5145
// ...
52-
use Symfony\Component\Notifier\Notification\Notification;
53-
use Symfony\Component\Notifier\NotifierInterface;
46+
use Symfony\Component\Notifier\ChatterInterface;
47+
use Symfony\Component\Notifier\Message\ChatMessage;
5448

5549
#[AsCommand(name: 'app:flash-sales:announce')]
5650
class AnnounceFlashSalesCommand extends Command
5751
{
58-
public function __construct(private NotifierInterface $notifier)
52+
public function __construct(private ChatterInterface $chatter)
5953
{
6054
parent::__construct();
6155
}
6256

6357
protected function execute(InputInterface $input, OutputInterface $output): int
6458
{
65-
$this->notifier->send(new Notification('Flash sales has been started!', ['chat/myMercureChatter']));
59+
$message = (new ChatMessage(
60+
'Flash sales has been started!',
61+
new MercureOptions(['/chat/flash-sales'])
62+
))->transport('myMercureChatter');
63+
64+
$this->chatter->send($message);
6665

6766
return 0;
6867
}
6968
}
7069

71-
Finally, to "listen" and trigger the notifications in the user's browser,
72-
call the ``stream_notifications()`` Twig function anywhere on the page:
70+
The ``chat/flash-sales`` is the Mercure topic the message will be sent to.
71+
The final step is to "listen" to that topic and trigger the notifications
72+
in the user's browser. To do that, call the ``stream_notifications()`` Twig
73+
function anywhere on the page:
7374

7475
.. code-block:: twig
7576
76-
{{ stream_notifications() }}
77-
{{ stream_notifications(['/my/topic/1', '/my/topic/2']) }}
77+
{{ stream_notifications(['/chat/flash-sales']) }}
7878
7979
.. note::
8080

81-
Calling ``stream_notifications()`` without parameter will fallback to the
82-
following unique topic: ``https://symfony.com/notifier``.
81+
Calling ``stream_notifications()`` without a parameter will default
82+
to the ``https://symfony.com/notifier`` topic.
8383

8484
Enjoy your server-sent native notifications!
8585

ux.symfony.com/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
3939
###< doctrine/doctrine-bundle ###
4040

4141
###> symfony/mercure-notifier ###
42-
MERCURE_DSN=mercure://default?topic=/demo/notifier
42+
MERCURE_DSN=mercure://default
4343
###< symfony/mercure-notifier ###

ux.symfony.com/config/packages/notifier.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
framework:
22
notifier:
33
chatter_transports:
4+
# MERCURE_DSN is defined in .env or should be set by you
45
custom_mercure_chatter_transport: '%env(MERCURE_DSN)%'
56
# slack: '%env(SLACK_DSN)%'
67
# telegram: '%env(TELEGRAM_DSN)%'

ux.symfony.com/src/Controller/NotifierController.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
use App\Form\SendNotificationForm;
66
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7-
use Symfony\Component\Asset\Packages;
87
use Symfony\Component\HttpFoundation\Request;
98
use Symfony\Component\HttpFoundation\Response;
10-
use Symfony\Component\Notifier\Notification\Notification;
11-
use Symfony\Component\Notifier\NotifierInterface;
9+
use Symfony\Component\Notifier\Bridge\Mercure\MercureOptions;
10+
use Symfony\Component\Notifier\ChatterInterface;
11+
use Symfony\Component\Notifier\Message\ChatMessage;
1212
use Symfony\Component\Routing\Annotation\Route;
1313

1414
class NotifierController extends AbstractController
1515
{
1616
#[Route('/notify', name: 'app_notify')]
17-
public function notify(Request $request, NotifierInterface $notifier): Response
17+
public function notify(Request $request, ChatterInterface $chatter): Response
1818
{
1919
$form = $this->createForm(SendNotificationForm::class);
2020

@@ -24,9 +24,11 @@ public function notify(Request $request, NotifierInterface $notifier): Response
2424
$message = SendNotificationForm::getTextChoices()[$form->getData()['message']];
2525

2626
// custom_mercure_chatter_transport is configured in config/packages/notifier.yaml
27-
// it ultimately points to "mercure://default?topic=/demo/notifier"
28-
$notification = new Notification($message, ['chat/custom_mercure_chatter_transport']);
29-
$notifier->send($notification);
27+
$message = (new ChatMessage(
28+
$message,
29+
new MercureOptions(['/demo/notifier'])
30+
))->transport('custom_mercure_chatter_transport');
31+
$chatter->send($message);
3032

3133
return $this->redirectToRoute('app_notify');
3234
}

0 commit comments

Comments
 (0)