# WebSockets and AnyCable

Run Action Cable as it is, or AnyCable inside your web container with Thruster

## Action Cable

Action Cable works with no setup. Caddy forwards `/cable` to your web container like any other request, on every web server.

Each deploy replaces your containers, so open connections close and Action Cable's client reconnects to the new container on its own.

## AnyCable with Thruster

[AnyCable Thruster](https://github.com/anycable/thruster) is Rails' Thruster with AnyCable built in. It answers WebSockets on `/cable` inside your web container and hands everything else to Puma, so there's no extra service to run and no Caddy config to write.

1. In your Gemfile, replace `gem "thruster"` with `gem "anycable-thruster"`, and add `gem "anycable-rails-core", "~> 1.6"` and `gem "redis"`. Run `bundle install`.
2. In `config/cable.yml`, set the production adapter to `any_cable`.
3. Start the server with Thruster. The Rails Dockerfile already does: `EXPOSE 80` and `CMD ["./bin/thrust", "./bin/rails", "server"]`. Run `bundle binstubs anycable-thruster` if you don't have `bin/thrust`.
4. Add these env vars to the app:

```
ANYCABLE_SECRET=<a long random string, like the output of bin/rails secret>
ANYCABLE_RPC_HOST=http://localhost:3000/_anycable
ANYCABLE_HTTP_RPC_MOUNT_PATH=/_anycable
ANYCABLE_BROADCAST_ADAPTER=redisx
ANYCABLE_PUBSUB=redis
ANYCABLE_REDIS_URL=<your REDIS_URL>
```

Then deploy.

AnyCable calls your channels' code (connecting, subscribing and actions like `perform`) over HTTP at `localhost:3000/_anycable`, which never leaves the container.

Every web container runs its own AnyCable, and your app broadcasts from its web requests, jobs and other processes, so broadcasts go through Redis to reach every container. Any Redis or Valkey in the cluster works (see *Databases and accessories*).

On a deploy, AnyCable tells each client it's restarting and to reconnect, and the client reconnects to the new container.

### Without channel code

If your app only uses Turbo Streams (`turbo_stream_from`) and no channels of its own, AnyCable can skip calling your app entirely, using JWT authentication and signed streams. Set it up as AnyCable's [signed streams](https://docs.anycable.io/anycable-go/signed_streams) and [JWT](https://docs.anycable.io/anycable-go/jwt_identification) docs describe, and set `ANYCABLE_NORPC=true`.

## AnyCable as an accessory

To run AnyCable on a server of its own instead, add a custom accessory:

- Image `anycable/anycable-go:1.6`, port `8080`, health check path `/health`
- Env vars:

```
ANYCABLE_HOST=0.0.0.0
ANYCABLE_PORT=8080
ANYCABLE_SECRET=<a long random string>
ANYCABLE_BROADCAST_ADAPTER=http
ANYCABLE_RPC_HOST=https://<your app's domain>/_anycable
```

Attach it to your app with a prefix other than `ANYCABLE`, like `ANYCABLE_GO`. The default prefix sets `ANYCABLE_HOST`, which the anycable gem reads too.

Give the app the same `ANYCABLE_SECRET`, plus:

```
ANYCABLE_HTTP_RPC_MOUNT_PATH=/_anycable
ANYCABLE_BROADCAST_ADAPTER=http
ANYCABLE_HTTP_BROADCAST_URL=http://<the accessory server's private IP>:8080/_broadcast
```

And send `/cable` to the accessory in the app's custom Caddyfile:

```
reverse_proxy /cable <the accessory server's private IP>:8080 {
    stream_close_delay 5m
}
%{default}
```

AnyCable calls your app through its public domain, so make sure nothing in front of the app (a firewall, Cloudflare's bot protection, or a rate limit like Rack::Attack) blocks requests from your servers' own IP to `/_anycable`. One AnyCable serves every web server in the cluster.