ux icon indicating copy to clipboard operation
ux copied to clipboard

[Turbo] add options to EventSource Mercure

Open ytilotti opened this issue 1 year ago • 7 comments

Q A
Bug fix? yes
New feature? n/a
Issues Fix #291
License MIT

To prevent 401 with Mercure & Turbo stream on different domain, add an options for EventSource is required. Exemple of code:

<div id="messages" {{ turbo_stream_listen('https://example.com/books/1', eventSourceOptions = {withCredentials: true}) }}</div>

ytilotti avatar Apr 24 '24 12:04 ytilotti

But I think the mercureAuthorization cookie is not sent. Is it normal ? It's the Twig Mercure extension that creates the cookie : https://github.com/symfony/mercure/blob/main/src/Twig/MercureExtension.php#L42.

tibobaldwin avatar May 16 '24 13:05 tibobaldwin

You have right @tibobaldwin for the first call, but after the Turbo takes over. if you are using private updates, Turbo needs to add withCredentials.

ytilotti avatar May 17 '24 12:05 ytilotti

But then, how do I add the mercure() Twig function?

tibobaldwin avatar May 17 '24 12:05 tibobaldwin

Great, this PR is very interesting :). But in fact, how to add this Twig helper which was added in https://github.com/symfony/mercure/pull/62?

seb-jean avatar May 18 '24 16:05 seb-jean

Sorry @tibobaldwin & @seb-jean, but I think it's not the place for these questions. Check the issue or the documentation?

But an example:

{% block javascripts %}
    <script>
        const eventSource = new EventSource("{{ mercure('https://example.com/books/1', { subscribe: ['https://example.com/books/1'] })|escape('js') }}", {
            withCredentials: true
        });
    </script>
{% endblock %}

{% block content %}
<div id="messages" {{ turbo_stream_listen('https://example.com/books/1', eventSourceOptions = {withCredentials: true}) }}
    Content
</div>
{% endblock %}

Without this PR, you need to create your own turbo stream controller and call like this:

...

{% block content %}
<div id="messages" data-controller="turbo-stream" data-turbo-stream-topic-value="https://example.com/books/1" data-turbo-stream-hub-value="{{ mercure() }}"
    Content
</div>
{% endblock %}

ytilotti avatar May 20 '24 16:05 ytilotti

Thanks. This is mainly to set the cookie in fact.

seb-jean avatar May 22 '24 07:05 seb-jean

Why this isn't merged yet? Seems totally fine to me, and very helpful.

gremo avatar Sep 01 '24 18:09 gremo

@ytilotti I think https://github.com/symfony/ux/pull/2447 answers your issue :).

seb-jean avatar Apr 12 '25 11:04 seb-jean

Indeed closing here as the feature is now implemented

smnandre avatar Apr 13 '25 02:04 smnandre