stimulusreflex / stimulus_reflex

Build reactive applications with the Rails tooling you already know and love.
https://docs.stimulusreflex.com
MIT License
2.28k stars 172 forks source link

Docs: Clarify forcing DOM update with authentication #123

Closed henrik closed 4 years ago

henrik commented 4 years ago

I followed https://docs.stimulusreflex.com/patterns#triggering-custom-events-and-forcing-dom-updates but it took me a while to figure out that since I've set up authentication per https://docs.stimulusreflex.com/authentication#encrypted-session-cookies, no one sees broadcasts to just StimulusReflex::Channel - I need to broadcast to e.g. StimulusReflex::Channel:abc123 where abc123 is a session ID.

But then I'd presumably need to loop over all sessions to broadcast to all users, which seems messy and a bad pattern.

I was going to make a doc PR, but I'm not sure what a good solution is here. I don't know Action Cable nor StimulusReflex well yet. Could it make sense for SR to (provide instructions to) set up both authed and non-authed channels/streams? Or would it be better if the docs said to set up a custom channel outside of SR for broadcasts to all users?

leastbad commented 4 years ago

Hey @henrik! You should join the SR Discord. :)

I am just reading through this and what you posted on the forum. Will reply soon.

leastbad commented 4 years ago

It's possible that https://github.com/hopsoft/stimulus_reflex/issues/25#issuecomment-591590662 will provide some insight.

What took me a while to figure out (poor ActionCable documentation) is that there's really two modes channels can operate in, stream_from and stream_for. Most people seem to think in terms of stream_from because it's the default that the generator creates. In some ways this is a shame because I actually find stream_for useful for all kinds of things.

Whereas stream_from is tied to a session id, stream_for is tied to a resource. For example, everyone looking at Post#29. If you want to send an individual User notifications, you can stream_for and broadcast to User#16.

Now, if you're trying to broadcast to everyone connected at the same time, you could probably figure out how to stream for a common object and have everyone subscribe to that object. For example, it might be possible to stream_for Rails, because why not? I haven't actually tried it. Let me know if it works!

henrik commented 4 years ago

Hi @leastbad - thanks so much for the thoughtful reply.

Clever idea setting up the subscription inside the Stimulus controller to save a level of indirection. I’ll certainly borrow that :)

I’ll read up further on the two kinds of streaming and experiment. Will try to take the time to improve the Action Cable docs when I understand those better.

And I’ll jump in the Discord on my next lab day! Not for a few weeks, probably.

leastbad commented 4 years ago

Just wanted to follow-up on this. Have you been able to revisit your project?

We are always looking to improve the documentation and lower the barrier to entry for those onboarding.

henrik commented 4 years ago

I'm afraid I haven't – Coronavirus wreaked havoc on our lab days. Might not happen for a while. Please feel free to close this ticket unless you plan on working on it independent of my efforts, and I can possibly revisit this later if and when I get back to it :)

leastbad commented 4 years ago

I hope that you and yours are well!

There’s no urgency to close anything. I was actually just checking in to see if I could be of service.

With OSS you do have to ask about issues that remain open, or else it starts to look like you’re not fixing problems with the library. Generally the Discord is actually the best way to get help.

henrik commented 4 years ago

Oh yeah, everyone's well, thankfully – just will affect priorities at work for a while. Hope you're well too.

Completely understand – you're doing a great job :)