@@ -40,46 +40,46 @@ properly configured notifier transport:
40
40
chatter_transports :
41
41
myMercureChatter : ' %env(MERCURE_DSN)%'
42
42
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
-
49
43
Then, you can inject the ``NotifierInterface `` service and send messages on the ``chat/myMercureChatter `` channel::
50
44
51
45
// ...
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 ;
54
48
55
49
#[AsCommand(name: 'app:flash-sales:announce')]
56
50
class AnnounceFlashSalesCommand extends Command
57
51
{
58
- public function __construct(private NotifierInterface $notifier )
52
+ public function __construct(private ChatterInterface $chatter )
59
53
{
60
54
parent::__construct();
61
55
}
62
56
63
57
protected function execute(InputInterface $input, OutputInterface $output): int
64
58
{
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);
66
65
67
66
return 0;
68
67
}
69
68
}
70
69
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:
73
74
74
75
.. code-block :: twig
75
76
76
- {{ stream_notifications() }}
77
- {{ stream_notifications(['/my/topic/1', '/my/topic/2']) }}
77
+ {{ stream_notifications(['/chat/flash-sales']) }}
78
78
79
79
.. note ::
80
80
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 .
83
83
84
84
Enjoy your server-sent native notifications!
85
85
0 commit comments