Turbo Streams intentionally restricts
official actions to CRUD related activity.
These official actions work well for a considerable number of use cases.
Try pushing Turbo Streams as far as possible before reaching for TurboReady.
If you find that CRUD isn't enough, TurboReady is there to handle pretty much everything else.
⚠️ TurboReady is intended for Rails apps that use Hotwire but not CableReady.
This is because CableReady already provides a rich set of powerful DOM operations.
If you add new capabilities to the browser, you can control them from the server.
// JavaScript on the clientimport morphdom from'morphdom'window.MyNamespace = {
morph: (from, to, options = {}) => {
morphdom(document.querySelector(from), to, options)
}
}
# Ruby on the server
turbo_stream.invoke "MyNamespace.morph",
args: [
"#demo",
"<div id='demo'><p>You've changed...</p></div>",
{children_only:true}
]
Implementation Details
There's basically one method to learn... invoke
# Ruby
turbo_stream
.invoke(method, args: [], selector:nil, camelize:true, id:nil)
# | | | | |# | | | | |- Identifies this invocation (optional)# | | | |# | | | |- Should we camelize the JavaScript stuff? (optional)# | | | (allows us to write snake_case in Ruby)# | | |# | | |- A CSS selector for the element(s) to target (optional)# | |# | |- The arguments to pass to the JavaScript method (optional)# |# |- The JavaScript method to invoke (can use dot notation)
📘 NOTE: The method will be invoked on all matching elements if a selector is present.
When this element enters the DOM,
Turbo Streams automatically executes invoke on the client with the template's JSON payload and then removes the element from the DOM.
Broadcasting
You can also broadcast DOM invocations to subscribed users.
# app/models/post.rbclassPost < ApplicationRecord
after_save do# emit a message in the browser conosle for anyone subscribed to this post
broadcast_invoke "console.log", args: ["Post was saved! #{to_gid.to_s}"]
# broadcast with a background job
broadcast_invoke_later "console.log", args: ["Post was saved! #{to_gid.to_s}"]
endend
# app/controllers/posts_controller.rbclassPostsController < ApplicationControllerdefcreate@post = Post.find params[:id]
if@post.update post_params
# emit a message in the browser conosle for anyone subscribed to this post@post.broadcast_invoke "console.log", args: ["Post was saved! #{to_gid.to_s}"]
# broadcast with a background job@post.broadcast_invoke_later "console.log", args: ["Post was saved! #{to_gid.to_s}"]
# you can also broadcast directly from the channelTurbo::StreamsChannel.broadcast_invoke_to @post, "console.log",
args: ["Post was saved! #{@post.to_gid.to_s}"]
# broadcast with a background jobTurbo::StreamsChannel.broadcast_invoke_later_to @post, "console.log",
args: ["Post was saved! #{@post.to_gid.to_s}"]
endendend
📘 NOTE:Method Chaining is not currently supported when broadcasting.
Background Job Queues
You may want to change the queue name for Turbo Stream background jobs in order to isolate, prioritize, and scale the workers independently.
No. But, perhaps it could be considered RJS's "modern" spirtual successor. 🤷♂️
Though it embraces JavaScript instead of trying to avoid it.
Does it use eval?
No. TurboReady can only invoke existing functions on the client.
It's not a carte blanche invitation to emit free-form JavaScript to be evaluated on the client.
A Word of Warning
TurboReady is a foundational tool designed to help you build modern, maintainable, and scalable reactive web apps with Hotwire.
It allows you to break free from the strict CRUD/REST conventions that Rails and Hotwire wisely encourage.
You should consider TurboReady a substrate for building additional libraries and abstractions.
Please don't use TurboReady to manually orchestrate micro DOM updates (from the server).
Such techniques are what gave rise to Full Stack Frontend and sent the industry on a decade long journey of complexity and frustration.
Community
Discord
Please join nearly 2000 of us on Discord for support getting started,
as well as active discussions around Rails, Hotwire, Stimulus, Turbo (Drive, Frames, Streams), TurboReady, CableReady, StimulusReflex, ViewComponent, Phlex, and more.
Be sure to introduce yourselves in the #newcomers channel!
Bump version number at lib/turbo_ready/version.rb. Pre-release versions use .preN
Run rake build and yarn build
Run bin/standardize
Commit and push changes to GitHub
Run rake release
Run yarn publish --no-git-tag-version
Yarn will prompt you for the new version. Pre-release versions use -preN
Commit and push changes to GitHub
Create a new release on GitHub (here) and generate the changelog for the stable release for it
License
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that turbo_ready demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 12 Dec 2022
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.