Provisioning Redis
Before proceeding, verify that your application is already set up to use Redis. Examine yourGemfile and look for the following lines:
bundle install. Rails will automatically have done this for you if it detected the redis-server executable on your machine at that time the application was created.
Now that Rails is ready to make use of Redis, lets deploy a redis cluster:
redis:. The URL you see will be considerably longer than the one
you see above. You will need to provide this URL to Rails, and with Fly.io this is done
via secrets. Run the following command replacing the url with the one from the output above:
Adding Turbo Streams to your Application
There are very few steps to make this work, writing very few lines of code Let’s start with theturbo_stream_for helper, which under the hood uses Turbo::StreamsChannel.
Modify app/views/names/index.html.erb to stream from “names”:
app/views/names/_name.html.erb to
identify the turbo frame:
app/controllers/names_controller.rb to broadcast changes as updates are made:
Deployment and testing
By now it should be no surprise that deployment is as easy asfly deploy and
fly apps open. Once that is done, copy the browser URL, open a second browser
window (it can even be a different browser or even on a different machine), and
paste the URL into the new window.
With one browser window open to the index page, use the other browser to change
one of the names. Once you click “Update name” the index list in the original
window will instantly update.
Of course, if this were a real application, inserting and removing names would
cause those changes to be broadcast. As they say, this is left as an exercise
for the student.