Ask A Question

Notifications

You’re not receiving notifications from this thread.

Introduction to Stimulus Reflex Discussion

Hi Chris! Excelent video, how can I prevent SR broadcast to all current connections? Is there a workaround for just stream to the same connection that trigger the reflex?

Reply

Chris, from what you mentioned in the video about stimulus_reflex, it would re-run an #index action (original page) after a reflex is executed. Wouldn't this be executing a lot of extra DB queries and thus using a lot more memory on servers?

I'm building something similar to a calendar app that displays a variety of events, and when I create a new event, I just need to insert that 1 record onto the page. If I re-ran the entire #index page, that would result in at least 5+ extra queries in most production applications (retrieve current_user, retrieve any user-specific items, query the #index action and possibly any secondary lookups, etc). From my experience the DB is the slowest part of most Rails apps, followed by the view rendering.

My approach currently is to use custom js.erb files with partials (and now view_components from Github) to dynamically update or insert my new events onto the calendar day. This means I only run the minimum DB queries necessary to insert the new record into the DB, and then spit back a little JS to the page that dynamically updates itself.

If the #index pages have a decent amount of content and thus would run several DB queries, is there really a benefit to stimulus_reflex since it automatically re-renders pages?

I can see cable_ready being useful, as it provides a nicer websocket interface to dynamically updating select portions of the page, and thus minimizing unnecessary DB queries.

BTW, I'd love to see some videos on Github's view_components, and get your thoughts on them! I started using them in a few places and I do like the ability to quickly test my views without having to load the entire app via system tests.

Reply

Dan, you're correct. If you trigger a reflex, it's just like refreshing the page. That's why I typically wouldn't use Reflex for situations like that where you need to be more efficient. That said, fragment caching can help considerably there. You just have to be careful with what you're doing.

That said, I think Stimulus Reflex killer for just getting something working quick and dirty as a prototype.

Reply

SR does allow you to update the whole page, selector or nothing. For the whole page morph it will re-run the controller action. Not sure how it would work with Calendar events but more info can be found here: https://docs.stimulusreflex.com/v/pre-release/rtfm/morph-modes

Reply

This is amazing

Reply

The is interesting, thanks for sharing. I guess I should look up stimulus :).

Question: What if the page was originally rendered as a post request? Will it repost the form data?

Reply

Does anyone have problems with loading this video? On yt it works fine :P

Reply

What happen if you need to add css class to html with stimulus? Example a button to turn on dark mode

Reply

Awesome. Looks incredible awesome.

Reply

I followed all your steps but I got this error:
Error invoking action "click->stimulus-reflex#__perform"
"The ActionCable channel subscription for StimulusReflex was rejected."

Anyone knnows how to solve this?

Reply

Solution: it seems that I had to restart the http server... in my case foreman

Reply

How do you guys test StimulusReflex code? All my user acceptance specs (even with js: true) stopped working and the SR code seems to never get called :(. I also tried to test the Stimulus controllers via Jest, but that doen't really work either. Any resources on how to correctly test SR and Stimulus?

Reply

I know Jason Charnes built a testing library for SR when they started using at Podia. Probably the tool to use: https://github.com/podia/stimulus_reflex_testing

Reply

cool stuff, this is the reason why blazor framework is such a hype in c# world right now, it allow you to the same stuff :) I worked a little bit with it and I am hyped to see that the same is there for rails

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,584+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.