If ActionCable isn't working properly in your environment, StimulusReflex cannot function.
Step one to any troubleshooting process should be "is it plugged in?"
rails generate channel test in your Rails project folder. This will ensure that your ActionCable setup has been initialized, although you should verify that in your
import 'channels' present.
Next, copy and paste the following into the two specified files, replacing their contents.
app/channels/test_channel.rbclass TestChannel < ApplicationCable::Channeldef subscribedstream_from "test"enddef receive(data)puts data["message"]ActionCable.server.broadcast("test", "ActionCable is connected")endend
If ActionCable is running properly, you should see
ActionCable is connected in your browser's Console Inspector and
Client is live in your server's STDOUT log stream.
You can feel free to remove both of these files after you're done, but leave
StimulusReflex.initialize() and share one ActionCable connection.
You might want to know the order in which your Reflexes are called, how long it took to process each Reflex or what the Reflex response payload contains. Luckily you can enable Reflex logging to your browser's Console Inspector.
There are two ways to enable client debugging in your StimulusReflex instance.
You can provide
debug: true to the initialize options like this:
You can also set debug mode after you've initialized StimulusReflex. This is especially useful if you just want to log the Reflex calls in your development environment:
By default, ActionCable emits particularly verbose Rails logger messages. You can optionally discard everything but exceptions by switching to the
warn log level, as is common in development environments:
config/environments/development.rb# :debug, :info, :warn, :error, :fatal, :unknownconfig.log_level = :warn
Alternatively, you can disable ActionCable logs at the framework level. This may improve performance, at the cost of not having ActionCable logs when you need them.
config/initializers/action_cable.rbActionCable.server.config.logger = Logger.new(nil)
If you're stuck working with legacy applications that impose constraints on your data attribute naming scheme, Stimulus and StimulusReflex give you a mechanism to provide an alternative schema.
In addition to controllerAttribute, actionAttribute and targetAttribute as defined by Stimulus, StimulusReflex adds the following: reflexAttribute, reflexPermanentAttribute, reflexRootAttribute and reflexDatasetAttribute.
You can update these values by providing your own schema to
In the above example, you have now configured your application to parse your DOM for
data-avenger attributes instead of
data-reflex attributes. 🦸
We're very proud of StimulusReflex and CableReady, which are both standing on the shoulders of many giants such as Rails, Ruby and Redis.
However, we want to be the first to recognize that there are limitations and constraints you should consider before using these technologies in your applications. After all, Rails itself does not claim to be immune from side effects, though huge amounts of effort are being invested into improving Ruby's concurrency story even while you read this.
There are edge cases where data could become out of date in between the time a Reflex queries the database and the resulting DOM update is applied in the browser. While there are mitigation strategies (such as tracking versions on everything) which you could employ, it's important to remember that you could be opening a can of worms that leads to sadness and/or madness.
Please don't use StimulusReflex or CableReady to drive mission-critical and/or life-threatening situations. It is a terrible choice to re-write your laser vision correction servo motor controls with StimulusReflex. Don't pilot drones or operate heavy machinery that is controlled with StimulusReflex.