Deploying Cabby
In production Cabby is one container or one binary next to your app. Whatever runs it, the same few things matter:
| What to do | |
|---|---|
| Ports | 8080 serves WebSockets (/cable), SSE and /health: publish it, behind TLS. Keep broadcasts off it with CABBY_HTTP_BROADCAST_PORT |
| Secret | The same CABBY_SECRET for your app and Cabby (Authentication) |
| Origins | CABBY_ALLOWED_ORIGINS: the hosts of the pages that may connect |
| Proxy | Pass the Upgrade and Connection headers on, and don't buffer SSE |
| History | A broker, if clients should catch up after a restart (History, recovery and presence); a shared one for several nodes |
| Open files | One per connection: raise the limit |
The Docker image (ghcr.io/danig2k/cabby:0.6.0) listens on 0.0.0.0, takes broadcasts over HTTP
only, runs as a non-root user and has a health check built in.
Kamal
Cabby runs as a Kamal accessory next to your app. This example uses HTTP RPC through cabby-rails
(the app mounts /_cabby) and a Kamal service called myapp:
# config/deploy.yml
env:
secret:
- CABBY_SECRET # the same name for the app and Cabby
accessories:
cabby:
image: ghcr.io/danig2k/cabby:0.6.0
host: 203.0.113.10 # a host of your web role, or a dedicated one
port: "8080:8080" # WebSocket /cable; terminate TLS in front of it
env:
clear:
CABBY_RPC_HOST: "https://app.example.com/_cabby" # your app's public URL
CABBY_ALLOWED_ORIGINS: "app.example.com" # restrict WebSocket origins
CABBY_BROADCAST_ADAPTER: http
CABBY_HTTP_BROADCAST_PORT: 8090 # broadcasts on 8090, kept inside the Docker network
secret:
- CABBY_SECRET
# .kamal/secrets
CABBY_SECRET=
Point the app's production settings at the accessory:
# config/cabby.yml
production:
<<: *default
url: wss://ws.example.com/cable
broadcast_url: http://myapp-cabby:8090/_broadcast # accessory container: <service>-<accessory>
The log has a line like Accept broadcast requests at http://0.0.0.0:8090/_broadcast (authorization required).
- Kamal puts accessories on its Docker network, so the app reaches Cabby at
myapp-cabby:8090, and port 8090 is never published. WithoutCABBY_HTTP_BROADCAST_PORT, a Cabby with a secret takes broadcasts on its main port, the published 8080. - RPC goes through the app's public URL, because Kamal app containers have no 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; check yours. - The image's health check reads the host and port from
CABBY_HOSTandCABBY_PORT. Change them with those variables, not with--hostand--port. - Deploying the app leaves the accessory running, so clients stay connected.
kamal accessory reboot cabbyrestarts Cabby itself.
An app on anycable-rails deploys the same way: see Migrating from AnyCable.
For several Cabby hosts sharing your app's database, see PostgreSQL.
Docker Compose
# compose.yaml
services:
web:
build: . # your app, listening on 3000
environment:
CABBY_SECRET: ${CABBY_SECRET:?set CABBY_SECRET}
CABBY_BROADCAST_URL: http://cabby:8090/_broadcast
cabby:
image: ghcr.io/danig2k/cabby:0.6.0
restart: unless-stopped
environment:
CABBY_SECRET: ${CABBY_SECRET:?set CABBY_SECRET}
CABBY_RPC_HOST: http://web:3000/_cabby
CABBY_HTTP_BROADCAST_PORT: 8090 # broadcasts stay on the Compose network
CABBY_ALLOWED_ORIGINS: app.example.com
CABBY_BROKER: sqlite
CABBY_SQLITE_PATH: /var/lib/cabby/cabby.db
ports:
- "8080:8080" # WebSocket /cable and /health; put TLS in front
volumes:
- cabby_data:/var/lib/cabby
ulimits:
nofile:
soft: 65536
hard: 65536
volumes:
cabby_data:
- The app reaches Cabby by its service name,
cabby, and Cabby reaches the app asweb. Only 8080 is published. CABBY_BROADCAST_URLstands for wherever your app keeps Cabby's broadcast URL (broadcast_urlinconfig/cabby.ymlfor Rails).- The SQLite broker keeps history and sessions in the named volume, so a restart of the
cabbyservice keeps them./var/lib/cabbybelongs to the image's non-root user.
Fly.io
On Fly.io, Cabby notices where it runs (FLY_APP_NAME, FLY_ALLOC_ID and FLY_REGION are set)
and applies its fly preset: it listens on 0.0.0.0 and takes HTTP broadcasts on its main port.
The preset also asks for embedded NATS, to connect the Machines, and the image has no
nats-server. So each Machine runs alone, and Cabby logs a warning that starts with
embedded NATS is off: the fly preset turns it on, but no nats-server executable was found.
For a single Machine, set CABBY_PUBSUB=none to say so and drop the warning. For several, give
them a Redis (CABBY_REDIS_URL, which the preset turns into Redis pub/sub) or see
NATS. A fly.toml for a separate Cabby app:
# fly.toml
app = "myapp-cabby"
[build]
image = "ghcr.io/danig2k/cabby:0.6.0"
[env]
CABBY_RPC_HOST = "https://myapp.fly.dev/_cabby"
CABBY_ALLOWED_ORIGINS = "myapp.fly.dev"
CABBY_PUBSUB = "none"
[http_service]
internal_port = 8080
force_https = true
With the preset, broadcasts arrive on 8080, the public port: the broadcast key from
CABBY_SECRET protects them. Your app sends them to https://myapp-cabby.fly.dev/_broadcast.
Other hosts
Any host that runs a container works the same way. What Cabby needs from it:
- WebSocket connections passed through to port 8080, or to the port in
PORT, which Cabby reads. - A health check on
GET /health. - One public port is enough: with a secret, broadcasts share it, protected by the broadcast key.
If the host has a private network, publish only 8080 and send broadcasts to
CABBY_HTTP_BROADCAST_PORTover that network. - A volume for
CABBY_SQLITE_PATHif you use the SQLite broker.
Behind a reverse proxy
A WebSocket starts as an HTTP request with Upgrade: websocket and Connection: Upgrade. A
proxy that drops either header turns it into a plain request, and Cabby answers
400 Bad Request, as it does to an upgrade over HTTP/1.0, which Nginx sends unless told
proxy_http_version 1.1. Cabby also needs the Origin header unchanged, to check it against
--allowed_origins.
Cabby closes a keep-alive connection that waits more than --idle_timeout seconds (620) for its
next request. Keep that above the proxy's or load balancer's idle timeout for backend connections,
so the proxy closes an idle connection first: otherwise a request can arrive just as Cabby closes
the connection, and the client gets a 502. AWS ALB and nginx upstream keep-alive use 60
seconds by default and Google Cloud load balancers 600, all below 620; raise --idle_timeout above
yours if it's longer. WebSocket connections and SSE responses aren't affected.
Caddy
Caddy passes WebSockets and SSE on by itself, and gets the certificate:
cable.example.com {
reverse_proxy cabby:8080
}
Nginx
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443 ssl;
server_name cable.example.com;
ssl_certificate /etc/ssl/cable.pem;
ssl_certificate_key /etc/ssl/cable.key;
location / {
proxy_pass http://cabby:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Cabby's SSE responses carry X-Accel-Buffering: no, which tells Nginx not to buffer them. Cabby
pings each client every 3 seconds (--ping_interval), so connections don't look idle to the
proxy.
Cloudflare
Behind Cloudflare's proxy, clients connect to Cloudflare with wss://, and Cabby needs what it
needs behind any proxy: the WebSocket upgrade passed through, the browser's Origin matching
--allowed_origins, and the pings above to keep connections busy. Keep the broadcast port off the
proxied hostname.
TLS
Most setups end TLS at the proxy. Cabby can also serve TLS itself:
Every port then speaks TLS (wss:// for WebSockets, https:// for /health and broadcasts) and
the log says Handle WebSocket connections at https://.... In the Docker image, set
CABBY_SSL_CERT and CABBY_SSL_KEY rather than the flags, so the health check uses TLS too.
Open files
Every connection holds a file descriptor, and Linux often allows 1024 per process. Cabby logs the limit it got when it starts:
INF Starting Cabby 0.6.0 (AnyCable 1.6.22 compatible) (pid: 1, open file limit: 1048576, gomaxprocs: 18)
Raise it where Cabby runs: ulimits in Compose (above), --ulimit nofile=65536:65536 with
docker run, or LimitNOFILE=65536 in a systemd unit:
# /etc/systemd/system/cabby.service
[Service]
ExecStart=/usr/local/bin/cabby --rpc_host=http://localhost:3000/_cabby --broadcast_adapter=http
EnvironmentFile=/etc/cabby/env
LimitNOFILE=65536
Restart=always
A burst of reconnects, after a restart or a network outage, waits in the system's queue of
connections to accept, which Cabby takes as long as the system allows: net.core.somaxconn on
Linux, 4096 on current kernels.
Graceful restarts
On SIGTERM or SIGINT, Cabby logs Shutting down..., tells every client
{"type":"disconnect","reason":"server_restart","reconnect":true} and closes their connections,
then finishes within --shutdown_timeout seconds (30). --shutdown_delay waits that many seconds
first. Clients reconnect on their own; what they get back depends on the broker.
Several nodes
Put the nodes behind a load balancer and give them a broker they all share. Each node delivers
to its own clients, so with several nodes they also need pub/sub (--pubsub) to pass broadcasts
on; --broker=redis picks Redis pub/sub for you.
--broker |
History and sessions | A client that lands on another node | Presence |
|---|---|---|---|
redis |
shared by every node | gets its session and the missed messages back | shared by every node |
postgres, nats |
shared by every node | gets its session and the missed messages back | none |
memory, sqlite |
each node keeps what it accepted | starts a new session, and can miss messages even when its history request succeeds | per node |
The in-memory and SQLite brokers are for one node. For presence on several, use the Redis broker: every node lists the same members, each user once.
- Restarts and upgrades. Restart one node at a time. With a shared broker, a
@cabby/webclient pauses for a second or two each time its node goes, then gets its session back and the messages of the pause, in order. A plain Action Cable client, such as@rails/actioncable, loses what was broadcast while it reconnected. - The load balancer checks
GET /healthevery second or two and stops sending to a node as soon as the check fails. Until then, it may send reconnecting clients to a node that's gone, and their pause gets longer./healthkeeps answering200during--shutdown_delay, so the delay alone doesn't take a node out. - Broadcasts sent to a node that's shutting down get a
503or a dropped connection. Send them to the load balancer, and send a failed one again: it lands on another node. - A node that loses Redis, NATS or PostgreSQL for a moment keeps its clients connected and doesn't tell them. With a shared broker, once its pub/sub connection is back, it reads what its streams missed and delivers it, once and in order; a link that stalls without breaking delays messages instead of losing them. With the in-memory or SQLite broker, what was broadcast meanwhile never reaches that node's clients. Presence events published meanwhile aren't recovered, though the Redis broker's member list stays right.