Skip to content

Commit b308e21

Browse files
committed
minor #878 [Notify] Using Chatter in the docs to show how to use MercureOptions to pass topics (weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- [Notify] Using Chatter in the docs to show how to use MercureOptions to pass topics | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Tickets | Fix #876 | License | MIT Hi! Using `NotifierInterface` is just a shortcut if you need to send a notification to multiple channels. In this case, Mercure only supports chatter types anyways. Using Chatter directly gives us control to choose the topics, which is much more realistic. Cheers! Commits ------- 4cbc4ca [Notify] Using Chatter in the docs to show how to use MercureOptions to pass topics
2 parents da799e5 + 4cbc4ca commit b308e21

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
@@ -42,46 +42,46 @@ properly configured notifier transport:
4242
chatter_transports:
4343
myMercureChatter: '%env(MERCURE_DSN)%'
4444
45-
.. note::
46-
47-
It is possible to specify the topics to send the notification in the ``MERCURE_DSN``
48-
environment variable by specifying the ``topics`` query parameter.
49-
Otherwise, notifications will be sent to ``https://symfony.com/notifier`` topic.
50-
5145
Then, you can inject the ``NotifierInterface`` service and send messages on the ``chat/myMercureChatter`` channel::
5246

5347
// ...
54-
use Symfony\Component\Notifier\Notification\Notification;
55-
use Symfony\Component\Notifier\NotifierInterface;
48+
use Symfony\Component\Notifier\ChatterInterface;
49+
use Symfony\Component\Notifier\Message\ChatMessage;
5650

5751
#[AsCommand(name: 'app:flash-sales:announce')]
5852
class AnnounceFlashSalesCommand extends Command
5953
{
60-
public function __construct(private NotifierInterface $notifier)
54+
public function __construct(private ChatterInterface $chatter)
6155
{
6256
parent::__construct();
6357
}
6458

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

6968
return 0;
7069
}
7170
}
7271

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

7677
.. code-block:: twig
7778
78-
{{ stream_notifications() }}
79-
{{ stream_notifications(['/my/topic/1', '/my/topic/2']) }}
79+
{{ stream_notifications(['/chat/flash-sales']) }}
8080
8181
.. note::
8282

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

8686
Enjoy your server-sent native notifications!
8787

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)