Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Turbo] add options to EventSource Mercure #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

Conversation

ytilotti
Copy link
Contributor

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 ytilotti reopened this Apr 24, 2024
@tibobaldwin
Copy link

tibobaldwin commented May 16, 2024

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.

@ytilotti
Copy link
Contributor Author

ytilotti commented May 17, 2024

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.

@tibobaldwin
Copy link

tibobaldwin commented May 17, 2024

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

@seb-jean
Copy link
Contributor

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

@ytilotti ytilotti requested a review from smnandre May 20, 2024 06:38
@ytilotti
Copy link
Contributor Author

ytilotti commented May 20, 2024

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 %}

@seb-jean
Copy link
Contributor

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

@gremo
Copy link
Contributor

gremo commented Sep 1, 2024

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

*/
public function renderTurboStreamListen(Environment $env, $topic): string;
public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot add parameter on an interface, even with default values, without breaking existing implementation.

interface FooInterface
{
    public function bar(string $a, array $eventSourceOptions = []): string;
}

class AppFoo implements FooInterface
{
    // existing method matching previous interface
    public function bar(string $a): string
    {
        return $a;
    }
}

This throws a PHP Fatal error as a the concrete implementation is not compatible anymore (see here).

Fatal error: Declaration of AppFoo::bar(string $a): string must be compatible with FooInterface::bar(string $a, array $eventSourceOptions = []): string in php-wasm run script on line 11

So to make this change in a BC way, you need:

  • to comment the new argument in the interface
  • to use func_get_args to fetch the value in the concrete class

You can see here a recent example of Symfony PR with similar BC layer: symfony/symfony#54531

Poke me if you need a hand here!

Copy link
Member

@smnandre smnandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very sorry @ytilotti i must have missed the re-review request.

I just made a comment about the BC break and how to solve it.

Also, could you add a test here ?

I think we will be good for the next release :)

@carsonbot carsonbot added Status: Needs Work Additional work is needed and removed Status: Needs Review Needs to be reviewed labels Sep 1, 2024
@seb-jean
Copy link
Contributor

@ytilotti I think #2447 answers your issue :).

@smnandre
Copy link
Member

Indeed closing here as the feature is now implemented

@smnandre smnandre closed this Apr 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Work Additional work is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[UX Turbo Mercure] EventSource is missing withCredentials
6 participants