Symfony UX Notify: desktop notifications for users with a Symfony page open, delivered through Mercure and the browser Notification API. Use for notification permissions, stream_notifications(), ChatterInterface, ChatMessage, MercureOptions and notify:connect.
191 stars
0 votes
0 copies
0 views
Added December 2, 2026
ai-agentsjavascriptgojavaphpbashapidocumentation
Works with
cli
api
Security analysis
A92/100
mediumInstalls packages at runtime which could introduce malicious dependencies
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Is that skill safe to install?
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
Are you the author of Ux Notify?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/smnandre-ux-notify)
---
name: ux-notify
description: 'Symfony UX Notify: desktop notifications for users with a Symfony page open, delivered through Mercure and the browser Notification API. Use for notification permissions, stream_notifications(), ChatterInterface, ChatMessage, MercureOptions and notify:connect.'
license: MIT
metadata:
author: Simon André
url: https://smnandre.dev
---
# UX Notify
Send a message from PHP with Symfony Notifier, and every browser listening on its Mercure topic shows a native system notification (the [Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API)). `stream_notifications()` renders the listener.
## Installation
```bash
composer require symfony/ux-notify
```
Requires StimulusBundle and a running Mercure hub. The package pulls in `symfony/mercure-bundle` and `symfony/mercure-notifier`. With Webpack Encore or Reprise, run `npm install --force` and rebuild.
```yaml
# config/packages/notifier.yaml
framework:
notifier:
chatter_transports:
mercure: '%env(MERCURE_DSN)%'
```
```bash
# .env
MERCURE_DSN=mercure://default # hub "default", default topic https://symfony.com/notifier
```
## Quick Reference
```
{{ stream_notifications() }} Listen on https://symfony.com/notifier
{{ stream_notifications('/sales') }} One topic
{{ stream_notifications(['/sales', '/news']) }} Several topics
new ChatMessage($title, new MercureOptions(...)) Send from PHP
```
## Sending
```php
use Symfony\Component\Notifier\Bridge\Mercure\MercureOptions;
use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Message\ChatMessage;
final readonly class FlashSaleAnnouncer
{
public function __construct(private ChatterInterface $chatter) {}
public function announce(): void
{
$message = new ChatMessage(
'Flash sale started!', // notification title
new MercureOptions(
topics: ['/sales'],
content: ['body' => '-30% on everything until midnight', 'icon' => '/images/sale.png'],
),
);
$this->chatter->send($message->transport('mercure'));
}
}
```
`content` becomes the [notification options](https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification#options): `body`, `icon`, `tag`, `badge`, `requireInteraction`... Every message needs a subject: it becomes the notification title.
## Listening
```twig
{# templates/base.html.twig, in <body> #}
{{ stream_notifications('/sales') }}
```
It renders an empty `<div>` with the `@symfony/ux-notify/notify` controller. The controller opens one `EventSource` per topic and closes them on disconnect. With no argument, it listens on `https://symfony.com/notifier`, the default topic of the Mercure transport: messages sent without `MercureOptions` arrive there.
## Permission
On the first message, the controller asks for permission while the user has yet to grant or deny it. Browsers increasingly ignore permission requests made without a user gesture, so ask from a click:
```javascript
// assets/controllers/notification_permission_controller.js
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
request() {
Notification.requestPermission();
}
}
```
```twig
<button data-controller="notification-permission" data-action="notification-permission#request">
Enable notifications
</button>
```
The Notifications API only runs in a secure context: HTTPS, or `localhost` in development.
## Extending
Pass your own Stimulus controller through `options`; it receives the event sources on `notify:connect`:
```twig
{{ stream_notifications('/sales', {'data-controller': 'sales-notify'}) }}
```
```javascript
// assets/controllers/sales_notify_controller.js
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
initialize() {
this.onConnect = this.onConnect.bind(this);
}
connect() {
this.element.addEventListener('notify:connect', this.onConnect);
}
disconnect() {
this.element.removeEventListener('notify:connect', this.onConnect);
}
onConnect(event) {
event.detail.eventSources.forEach((source) => {
source.addEventListener('message', (message) => {
const { summary, content } = JSON.parse(message.data);
// e.g. also show an in-page toast
});
});
}
}
```
Of `options`, only `data-controller` is read: put other attributes on a wrapper element.
## Configuration
```yaml
# config/packages/notify.yaml
notify:
mercure_hub: mercure.hub.default # Mercure hub service ID
```
Since Symfony UX 3.6+, Notify follows the hub's `protocol_version` (MercureBundle 0.6 defaults to `1.0`) and subscribes with `match` or `topic` accordingly.
## Gotchas
- Keep messages public (`private: false`, the default): the `EventSource` connects without credentials, so it receives public updates only.
- Leave the `type` option of `MercureOptions` empty: the controller listens to the default `message` event only.
- Nothing appears while the tab's site lacks permission, or when the OS blocks notifications for the browser (check the system settings).
- For in-page updates (a toast, a counter), broadcast Turbo Streams with the turbo skill: Notify targets the operating system.
## References
- [Symfony UX package page](https://ux.symfony.com/notify) ([Markdown summary](https://ux.symfony.com/notify.md)) · [`symfony/ux-notify` on Packagist](https://packagist.org/packages/symfony/ux-notify).
- Try the [browser notifications demo](https://ux.symfony.com/notify#demo).
- For Mercure transport, browser setup and controller events, see the [Symfony UX Notify documentation](https://symfony.com/bundles/ux-notify).
## See Also
- **turbo**: Mercure broadcasts that update the page itself
- **stimulus**: custom controllers on `notify:connect`