Rails
Cabby takes over the WebSocket connections that Action Cable holds inside your Puma processes. Rails is only called when a client connects, subscribes or sends a message, through a small RPC endpoint mounted in your app.
Cabby speaks the same protocol as AnyCable, so it uses the anycable-rails-core gem. Your
channels, ApplicationCable::Connection and broadcasts don't change, and you're never locked in:
the gem works with either server.
1. Add the gem
anycable-rails-core is the Rails side that Cabby talks to over HTTP.
2. Switch the Action Cable adapter
# config/cable.yml
development:
adapter: any_cable
test:
adapter: test
production:
adapter: any_cable
3. Configure the connection
# config/anycable.yml
default:
http_rpc: true # mounts the RPC endpoint at /_anycable
broadcast_adapter: http # broadcasts are POSTed to Cabby
http_broadcast_url: http://localhost:8080/_broadcast
websocket_url: ws://localhost:8080/cable
development:
<<: *default
secret: change-me
production:
<<: *default
http_broadcast_url: http://cabby.internal:8080/_broadcast # where your app reaches Cabby
websocket_url: wss://cable.example.com/cable # where browsers connect
In production, set the secret in the ANYCABLE_SECRET environment variable. Make it a long
random string; bin/rails secret prints one. Don't put it in config/credentials.yml.enc.
Rails reads that file in development too, so it would replace change-me there. Start Cabby with
the same value in --secret.
4. Point the browser at Cabby
Make sure your layout has the Action Cable meta tag. The @rails/actioncable client and Turbo
read the WebSocket URL from it:
5. Run Cabby with your app
Install Cabby if you haven't yet. Then add it to Procfile.dev so bin/dev starts
it:
ws: cabby --port=8080 --rpc_host=http://localhost:3000/_anycable --secret=change-me --broadcast_adapter=http
bin/dev gives each process its own PORT, and Cabby listens on PORT when it's set.
--port=8080 keeps Cabby on 8080.
No Procfile.dev? Run the command in a second terminal.
Start everything:
Your channels now run through Cabby. Broadcasts work as before:
ActionCable.server.broadcast("chat", { text: "Hello" })
Broadcasts are sent from a background thread. A one-off bin/rails runner script can exit
before the message goes out, so end it with a short sleep 1.
Deploying with Kamal
Run the Cabby Docker image as a Kamal accessory next to your app:
# config/deploy.yml
env:
secret:
- ANYCABLE_SECRET # the Rails gem reads ANYCABLE_*
accessories:
cabby:
image: ghcr.io/danig2k/cabby:0.1.0
host: 203.0.113.10 # the host your web role runs on
port: "8080:8080" # WebSocket /cable; put TLS in front of it
env:
clear:
ANYCABLE_RPC_HOST: "https://app.example.com/_anycable" # your app's public URL
ANYCABLE_HTTP_BROADCAST_PORT: 8090 # broadcasts stay on the Docker network, off the published port
ANYCABLE_BROADCAST_ADAPTER: http # HTTP broadcasts only, no Redis needed
secret:
- ANYCABLE_SECRET
# .kamal/secrets
ANYCABLE_SECRET=
The top-level env: passes ANYCABLE_SECRET to your Rails containers, and the accessory's
env: passes the same value to Cabby.
Point the production URLs in config/anycable.yml at the accessory:
# config/anycable.yml
production:
<<: *default
http_broadcast_url: http://myapp-cabby:8090/_broadcast # accessory container: <service>-<accessory>
websocket_url: wss://cable.example.com/cable
Then run kamal accessory boot cabby.
- Kamal puts accessories on its Docker network, so your app reaches Cabby at
myapp-cabby:8090and the broadcast port is never published. - Keep
ANYCABLE_HTTP_BROADCAST_PORT. Without it, Cabby takes broadcasts on its main port when a secret is set, and that's the published port 8080. - Kamal's Docker network is per host. If Cabby runs on a separate host, publish port 8090 on a
private address only and put that address in
http_broadcast_url. - RPC goes through your app's public URL because Kamal app containers don't have a fixed name.
- For
wss://, put a TLS proxy in front of port 8080. Recent Kamal versions can route an accessory through kamal-proxy with aproxy:block. - If your connection identifies users by cookie, set the cookie's domain so
cable.example.comreceives it, for exampleconfig.session_store :cookie_store, key: "_myapp_session", domain: :all.
Using Turbo Streams? See Hotwire.