Advanced Use
How to get the most out of StimulusReflex
ActionCable
StimulusReflex leverages Rails ActionCable. Understanding what Rails provides out of the box will help you get the most value from this library.
Performance
ActionCable emits verbose log messages. Disabling ActionCable logs may improve performance.
ActionCable.server.config.logger = Logger.new(nil)Rooms
You might find the need to restrict communication to a specific room. This can be accomplished in 2 ways.
Passing the room name as an option to
register.
export default class extends Controller {
connect() {
StimulusReflex.register(this, { room: 'ExampleRoom12345' });
}
}Setting the
data-roomattribute on the StimulusController element.
Setting room in the body with a data attribute can pose a security risk. Consider assigning room when registering the Stimulus controller instead.
Stimulus Controllers
Render Delay
An attempt is made to reduce repaint jitter when users trigger several updates in succession.
You can control how long to wait prior to updating the DOM - think debounce. Simply set the renderDelay option in milliseconds when registering the controller.
Reflexes
Server side reflexes inherit from StimulusReflex::Reflex and hold logic responsible for performing operations like writing to your backend data stores. Reflexes are not concerned with rendering because rendering is delegated to the Rails controller and action that originally rendered the page.
Properties
connection- the ActionCable connectionchannel- the ActionCable channelrequest- anActionDispatch::Requestproxy for the socket connectionsession- theActionDispatch::Sessionstore for the current visitorurl- the URL of the page that triggered the reflexelement- a Hash like object that represents the HTML element that triggered the reflex
element
elementThe element property contains all of the Stimulus controller's DOM element attributes as well as other properties like checked and value.
Here's an example that outlines how you can interact with the element property in your reflexes.
Last updated
Was this helpful?