Installation
TIP
If you're running StimulusReflex or other CableReady-powered libraries (such as Optimism or Futurism), CableReady is already installed and you can move on to Hello World. 🎉
Client and Server Packages
CableReady has both client (npm package) and server (Ruby gem) components which need to be installed. It is vitally important that you make sure that your server and client versions match exactly.
DANGER
There can be issues with conflicting versions introduced when we're testing a pre-release build, since yarn will typically install the most recent package available, while Rubygems will install the last official release. 🤯
bundle add cable_ready
yarn add cable_ready
You can manually tweak and/or lock the versions you want to install by modifying Gemfile
and package.json
respectively, then re-running bundle install && yarn install
.
cable_ready_stream_from
Helper
CableReady v5 introduces the cable_ready_stream_from
helper, which allows Rails developers to broadcast operations to DOM targets without having to do any manual Channel setup.
You should only need to run this once:
rails generate cable_ready:stream_from
If there's any issue, you need to make sure that your index.js
or application.js
includes the following:
import consumer from '../channels/consumer'
import CableReady from 'cable_ready'
CableReady.initialize({ consumer })
CableReady Initializer
CableReady supports an optional Rails initializer which, among other things, allows you to declare custom operations. We provide a generator to create a handy blank initializer which has all of the options listed as comments:
rails generate cable_ready:initializer
Upgrading, package versions and sanity
When upgrading CableReady, it's very important that you make sure your gem version and npm package versions match.
Since mismatched versions are the first step on the path to hell, by default CableReady won't allow the server to start if your versions are mismatched in the development environment.
If you have special needs, you can override this setting in your initializer. :warn
will emit the same text-based warning but not prevent the server process from starting. :ignore
will silence all mismatched version warnings.
CableReady.configure do |config|
config.on_failed_sanity_checks = :warn
end
Upgrading to v5.0.0
- git repos are now living in the stimulusreflex organization on GitHub
- make sure that you update
cable_ready
to5.0.0
in both yourGemfile
andpackage.json
- create an initializer with
rails generate cable_ready:initializer
if needed - install
cable_ready_stream_from
support withrails generate cable_ready:stream_from
- install the
@cable_ready/audio_operations
npm package if required - convert your custom operations to use the new
CableReady.operations
object
ActionCable
CableReady depends on the ActionCable framework (installed by default as part of Ruby on Rails) to handle sending data to the client over websockets. You must have ActionCable installed on both the client and server... and it will be unless you've disabled it.
You can check your package.json
to verify that @rails/actioncable
is installed. If you have trouble with ActionCable, consider verifying that it's installed correctly.
AnyCable
If you are preparing to deploy your site into production, you are advised to consider using AnyCable or AnyCable Pro, which boasts lower memory consumption, Apollo GraphQL compatibility and support for a binary transport protocol.
Redis
Redis for everything: caching, sessions, jobs and yes, ActionCable. Redis is a dependency for StimulusReflex; the installation task sets up Redis for ActionCable's subscription adapter in the development environment.
While Redis in development is not mandatory for standalone CableReady use, we do recommend it:
gem "redis", ">= 4.0", :require => ["redis", "redis/connection/hiredis"]
gem "hiredis"
development:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: your_application_development
Rails 5.2
The CableReady documentation assumes that you are running Rails 6.x. It is possible to use CableReady with Rails 5.2, but you must make some adjustments to your configuration.
- Replace
actioncable
with@rails/actioncable
inpackage.json
yarn remove actioncable
yarn add @rails/actioncable
- Replace any instance of
import Actioncable from "actioncable"
withimport { createConsumer } from "@rails/actioncable"
- This imports the
createConsumer
function directly - Previously, you might call
createConsumer()
on theActioncable
import:Actioncable.createConsumer()
- Now, you can reference
createConsumer()
directly
- This imports the
Polyfills for IE11
If you need to provide support for older browsers, you can yarn add @cable_ready/polyfills
and include them before your Stimulus controllers (if any) and CableReady channels:
// other stuff
import '@cable_ready/polyfills'
import 'channels'
import 'controllers'