Claude Skill

ux-notify

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.

LLM Mart · 0 points · 0 views 0 listing impressions 0 install-command copies
Download smnandre-symfony-ux-skills-skills_ux-notify-78c25ed.zip · 2 KB

Install

skills CLI npx skills add https://github.com/smnandre/symfony-ux-skills/tree/main/skills/ux-notify
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install smnandre-symfony-ux-skills@llmmart
Git git clone https://github.com/smnandre/symfony-ux-skills.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole smnandre/symfony-ux-skills collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

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). stream_notifications() renders the listener.

Installation

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.

# config/packages/notifier.yaml
framework:
    notifier:
        chatter_transports:
            mercure: '%env(MERCURE_DSN)%'
# .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

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: body, icon, tag, badge, requireInteraction... Every message needs a subject: it becomes the notification title.

Listening

{# 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:

// assets/controllers/notification_permission_controller.js
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
    request() {
        Notification.requestPermission();
    }
}
<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:

{{ stream_notifications('/sales', {'data-controller': 'sales-notify'}) }}
// 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

# 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

See Also

  • turbo: Mercure broadcasts that update the page itself
  • stimulus: custom controllers on notify:connect
Files (symfony-ux-skills)
  • SKILL.md 5.9 KB
    ---
    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`
    

Comments

Sign in to join the conversation.

No comments yet. Start the conversation.

Reviews (0)

No reviews yet.