Skip to content
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

Add options to turbo_stream_listen #2447

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from

Conversation

Fan2Shrek
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
Issues Fix #1860 I think
License MIT

This allows to pass options to the EventSource, for example
{{ turbo_stream_listen('a_topic', 'default', { withCredentials: true }) }}
Or create specific auth cookie
{{ turbo_stream_listen('a_topic', 'default', { subscribe: '*' }) }}
This functionality is similar to how mercure() works

@carsonbot carsonbot added Feature New Feature Status: Needs Review Needs to be reviewed labels Dec 12, 2024
Copy link

github-actions bot commented Dec 12, 2024

📊 Packages dist files size difference

Thanks for the PR! Here is the difference in size of the packages dist files between the base branch and the PR.
Please review the changes and make sure they are expected.

FileBefore (Size / Gzip)After (Size / Gzip)
Turbo
turbo_stream_controller.d.ts 562 B / 280 B 651 B+16% 📈 / 303 B+8% 📈
turbo_stream_controller.js 1.27 kB / 535 B 1.35 kB+6% 📈 / 565 B+6% 📈

@Fan2Shrek Fan2Shrek force-pushed the add-option-to-turbo-stream branch 3 times, most recently from 867707d to 06ac699 Compare December 12, 2024 22:35
src/Turbo/CHANGELOG.md Outdated Show resolved Hide resolved
unset($options['subscribe'], $options['publish'], $options['additionalClaims']);
}

return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic, $options);
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 pass options as third parameter, as the interface only declare "options" in docblock.

Copy link
Contributor Author

@Fan2Shrek Fan2Shrek Dec 13, 2024

Choose a reason for hiding this comment

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

I'm not really sure how to do that, could you explain it to me ?

@@ -12,19 +12,24 @@
namespace Symfony\UX\Turbo\Twig;

use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mercure\Authorization;
use Symfony\UX\Turbo\Bridge\Mercure\TopicSet;
Copy link
Member

Choose a reason for hiding this comment

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

I think you could/should probably use TopicSet here, as it's specific to Mercure and would keep things properly typed / isolated.

Or any strategy that does not impact users using a different solution than Mercure.

You should look at this PR and the discussion, as most challenges are identical

#2407

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see where I could use this object, could you add more details ?

Copy link
Member

Choose a reason for hiding this comment

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

I did not mean "use this one", but if you read the PR linked and the discussion, you will see how it relates to yours.

We decided to introduce a new object to bypass these BC questions.

@carsonbot carsonbot added Status: Needs Work Additional work is needed and removed Status: Needs Review Needs to be reviewed labels Dec 13, 2024
@Fan2Shrek Fan2Shrek force-pushed the add-option-to-turbo-stream branch from 06ac699 to abcc723 Compare December 13, 2024 18:02
@carsonbot carsonbot added Status: Needs Review Needs to be reviewed and removed Status: Needs Work Additional work is needed labels Dec 13, 2024
@Fan2Shrek Fan2Shrek force-pushed the add-option-to-turbo-stream branch from abcc723 to e95cbab Compare December 13, 2024 18:14
@Fan2Shrek Fan2Shrek force-pushed the add-option-to-turbo-stream branch 3 times, most recently from f92e643 to e56b74c Compare December 14, 2024 22:53
@Fan2Shrek
Copy link
Contributor Author

Hey @smnandre,
Thanks for taking the time to explain this to me ! 😊

However, the last commit seems a bit off to me. Could you confirm if that's what you had in mind ?
Also, since the cookie logic was moved to the Mercure TurboStreamListenRenderer, is the Twig runtime still relevant ?

Comment on lines +73 to +79
$this->authorization->setCookie(
$request,
$eventSourceOptions['subscribe'] ?? [],
$eventSourceOptions['publish'] ?? [],
$eventSourceOptions['additionalClaims'] ?? [],
$eventSourceOptions['transport'] ?? null,
);
Copy link
Member

Choose a reason for hiding this comment

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

This will throw if the method is called twice during the render of the page.

What are the goal of the various options here ?

Or, say differently, why not use the mercure extension here ? (real question i'm not sure to get)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean the Mercure Twig extension?
If so, it doesn't seem to work 😅. The dependency isn't being resolved, and to be honest, I'm not sure why.

@smnandre
Copy link
Member

Hi seb-jean Guervyl gremo DRaichev and anyone who want to :)

Well.... It’s time to lay all this out clearly! 😊

Could you share your ideal Developer Experience (DX) suggestions?
As much in PHP code than in Twig // Give examples of code usage
Once we have a solid understanding of what you’re aiming for, we can figure out the best way to make it happen together.

Please clarify where and how you’d use this feature:
For example, in a page template, a component, a full-page view, multiple times, or other contexts. This will help us better target the needs and define responsibilities.

Looking forward to your insights!

@smnandre
Copy link
Member

@norkunas as i think you are one of the (if not the) person knowing this most, and having experience with this BC problematic... i'd love to have your input on this.

@Fan2Shrek I've opened a meta-issue to see how we can make changes in a more generic way first, to ease the development of this specific need, but also others after.

--> #2460

@seb-jean
Copy link
Contributor

seb-jean commented Dec 21, 2024

Hi,

This feature would be interesting for me: I would use it with #327.

Let's take the example of GitHub notifications.

I have a Notification entity:

// src/Entity/Notification.php
namespace App\Entity;

use Symfony\UX\Turbo\Attribute\Broadcast;

#[Broadcast(topics: ['@="notifications_list_" ~ entity.getUser().getId()'], private: true)]
class Notification
{
    // ...
}

With each creation, a notification will be automatically added with the Broadcast system.

A user can then view their own notifications with {{ turbo_stream_listen('notifications_list_' ~ app.user.id) }}.

The generated code will be as follows:

<div data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="https://symfony.com/.well-known/mercure" data-symfony--ux-turbo--mercure-turbo-stream-topic-value="notifications_list_1"></div>

Unfortunately, this is not secure because a malicious person will be able to change the value of the data-symfony--ux-turbo--mercure-turbo-stream-topic-value attribute to have notifications from another user.

Here I talked about notifications, but it could also be valid for instant messaging for example.

The idea would then be to secure the turbo_stream_listen().

@smnandre
Copy link
Member

Let's add this to the conversatioon about stream sources : #2460

I'd like to keep talking about generic implementation first (but based on your specific need, it's a rich source of feedback / irl usage!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX Feature New Feature Status: Needs Review Needs to be reviewed Status: Needs Work Additional work is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Turbo] Add mercure() to turbo_stream_listen()
4 participants