[](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/) [](https://codeclimate.com/github/hopsoft/stimulus_reflex/maintainability) # StimulusReflex _reflex_ - an action that is performed as a response to a stimulus ### Build reactive [Single Page Applications (SPAs)](https://en.wikipedia.org/wiki/Single-page_application) with [Rails](https://rubyonrails.org) and [Stimulus](https://stimulusjs.org) This project supports building [reactive applications](https://en.wikipedia.org/wiki/Reactive_programming) with the Rails tooling you already know and love. It's designed to work perfectly with [server rendered HTML](https://guides.rubyonrails.org/action_view_overview.html), [Russian doll caching](https://edgeguides.rubyonrails.org/caching_with_rails.html#russian-doll-caching), [Stimulus](https://stimulusjs.org), [Turbolinks](https://www.youtube.com/watch?v=SWEts0rlezA), etc... __No need for a complex front-end framework. No need to grow your team or duplicate your efforts.__ _Inspired by [Phoenix LiveView](https://youtu.be/Z2DU0qLfPIY?t=670)._ 🙌 ## Table of Contents - [Before you Begin](#before-you-begin) - [How it Works](#how-it-works) - [Setup](#setup) * [JavaScript](#javascript) * [Gemfile](#gemfile) - [Basic Usage](#basic-usage) * [app/views/pages/example.html.erb](#appviewspagesexamplehtmlerb) * [app/javascript/controllers/example.js](#appjavascriptcontrollersexamplejs) * [app/reflexes/example_reflex.rb](#appreflexesexample_reflexrb) - [Advanced Usage](#advanced-usage) * [Reflex Methods](#reflex-methods) + [The `options` Keyword Argument](#the-options-keyword-argument) * [ActionCable](#actioncable) + [Performance](#performance) + [ActionCable Rooms](#actioncable-rooms) * [Render Delay](#render-delay) - [Demo Applications](#demo-applications) - [Contributing](#contributing) * [Coding Standards](#coding-standards) * [Releasing](#releasing) ## Before you Begin StimulusReflex provides functionality similar to what can already be achieved with Rails by combining [UJS remote elements](https://guides.rubyonrails.org/working_with_javascript_in_rails.html#remote-elements) , [Stimulus](https://stimulusjs.org), and [Turbolinks](https://github.com/turbolinks/turbolinks). _Consider building with standard Rails tooling before introducing StimulusReflex._ _Check out the [Stimulus TodoMVC](https://github.com/hopsoft/stimulus_todomvc) example if you are unsure how to accomplish this._ StimulusReflex offers 3 primary benefits over the traditional Rails HTTP request/response cycle. 1. __Communication happens on the ActionCable web socket__ _- saves time by avoiding the overhead of establishishing traditional HTTP connections_ 1. __The controller action is invoked directly__ _- skips framework overhead such as the middleware chain, etc..._ 1. __DOM diffing is used to update the page__ _- provides faster rendering and less jitter_ ## How it Works 1. Render a standard Rails view template 1. Use [Stimulus](https://stimulusjs.org) and [ActionCable](https://edgeguides.rubyonrails.org/action_cable_overview.html) to invoke a method on the server 1. Watch the page automatically render updates via fast [DOM diffing](https://github.com/patrick-steele-idem/morphdom) 1. That's it... __Yes, it really is that simple.__ There are no hidden gotchas.  ## Setup ### JavaScript ``` yarn add stimulus_reflex ``` ### Gemfile ```ruby gem "stimulus_reflex" ``` ## Basic Usage ### app/views/pages/example.html.erb ```erb
Increment <%= @count.to_i %>