anycable-rails-core 1.6.2 → 1.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/MIT-LICENSE +1 -1
- data/README.md +12 -0
- data/lib/anycable/rails/channel/presence.rb +1 -1
- data/lib/anycable/rails/version.rb +1 -1
- data/lib/generators/anycable/bin/USAGE +3 -1
- data/lib/generators/anycable/bin/bin_generator.rb +7 -6
- data/lib/generators/anycable/bin/templates/bin/anycable-go.tt +4 -3
- data/lib/generators/anycable/download/download_generator.rb +2 -11
- data/lib/generators/anycable/setup/USAGE +8 -0
- data/lib/generators/anycable/setup/setup_generator.rb +104 -68
- data/lib/generators/anycable/setup/templates/Procfile.dev.tt +1 -1
- data/lib/generators/anycable/with_release_helpers.rb +69 -0
- data/lib/puma/plugin/anycable.rb +171 -0
- data/skills/anycable-rails/SKILL.md +82 -0
- data/skills/anycable-rails/references/deployment.md +187 -0
- data/skills/anycable-rails/references/hotwire.md +269 -0
- data/skills/anycable-rails/references/installation.md +292 -0
- data/skills/anycable-rails/references/live-checks.md +129 -0
- data/skills/anycable-rails/references/personalization.md +114 -0
- data/skills/anycable-rails/references/presence.md +240 -0
- data/skills/anycable-rails/references/realtime-features.md +231 -0
- data/skills/anycable-rails/references/testing.md +145 -0
- data/skills/anycable-rails/references/troubleshooting.md +128 -0
- data/skills/anycable-rails/scenarios/channel.yml +51 -0
- data/skills/anycable-rails/scenarios/signed-stream.yml +38 -0
- metadata +18 -3
- data/lib/generators/anycable/setup/templates/bin/anycable-go.tt +0 -27
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "puma/plugin"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "shellwords"
|
|
6
|
+
|
|
7
|
+
# Puma plugin to run AnyCable server together with Puma (for development).
|
|
8
|
+
#
|
|
9
|
+
# Usage (config/puma.rb):
|
|
10
|
+
#
|
|
11
|
+
# plugin :anycable if ENV["ANYCABLE_IN_PUMA"]
|
|
12
|
+
#
|
|
13
|
+
# # Optional settings (put them after the `plugin` call):
|
|
14
|
+
# anycable_command "bin/anycable-go --port=8080"
|
|
15
|
+
# anycable_log "log/anycable.log"
|
|
16
|
+
# anycable_pidfile "tmp/pids/anycable.pid"
|
|
17
|
+
#
|
|
18
|
+
# The plugin starts the server after Puma boots and stops it when Puma stops.
|
|
19
|
+
# The server stays alive during Puma restarts (`tmp_restart`, `bin/rails restart`).
|
|
20
|
+
# A server left by a killed Puma process is stopped on the next start.
|
|
21
|
+
module Puma
|
|
22
|
+
class DSL
|
|
23
|
+
# The command to run AnyCable server (default: "bin/anycable-go")
|
|
24
|
+
def anycable_command(command)
|
|
25
|
+
@options[:anycable_command] = command
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The file to write AnyCable server output to (default: Puma output)
|
|
29
|
+
def anycable_log(path)
|
|
30
|
+
@options[:anycable_log] = path
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The file to keep AnyCable server PID in (default: "tmp/pids/anycable.pid")
|
|
34
|
+
def anycable_pidfile(path)
|
|
35
|
+
@options[:anycable_pidfile] = path
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
Puma::Plugin.create do
|
|
41
|
+
attr_reader :log_writer, :options, :anycable_pid
|
|
42
|
+
|
|
43
|
+
def start(launcher)
|
|
44
|
+
@log_writer = launcher.log_writer
|
|
45
|
+
@options = launcher.options
|
|
46
|
+
|
|
47
|
+
events = launcher.events
|
|
48
|
+
|
|
49
|
+
# Puma 7+ renamed on_* events to after_*
|
|
50
|
+
if events.respond_to?(:after_booted)
|
|
51
|
+
events.after_booted { start_anycable }
|
|
52
|
+
events.after_stopped { stop_anycable }
|
|
53
|
+
else
|
|
54
|
+
events.on_booted { start_anycable }
|
|
55
|
+
events.on_stopped { stop_anycable }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def start_anycable
|
|
62
|
+
@anycable_pid = running_anycable_pid || spawn_anycable
|
|
63
|
+
return unless anycable_pid
|
|
64
|
+
|
|
65
|
+
waiter = Process.detach(anycable_pid)
|
|
66
|
+
|
|
67
|
+
# Note: blocks added via `in_background` after boot never run, so we use a thread
|
|
68
|
+
Thread.new { monitor_anycable(waiter) }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def spawn_anycable
|
|
72
|
+
# Use a separate process group to control the shutdown order:
|
|
73
|
+
# Ctrl-C must not reach the server before Puma stops.
|
|
74
|
+
pid = Process.spawn(*command, pgroup: true, **output_options)
|
|
75
|
+
|
|
76
|
+
# Puma resets ENV on restart, so we use a file to keep the PID between restarts
|
|
77
|
+
FileUtils.mkdir_p(File.dirname(pidfile))
|
|
78
|
+
File.write(pidfile, pid)
|
|
79
|
+
|
|
80
|
+
log "* Starting AnyCable server (pid: #{pid})..."
|
|
81
|
+
pid
|
|
82
|
+
rescue SystemCallError => e
|
|
83
|
+
log "! Failed to start AnyCable server (#{command.join(" ")}): #{e.message}"
|
|
84
|
+
nil
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Returns the PID of the server started by this Puma process before a restart.
|
|
88
|
+
# A server left by another (killed) Puma process is stopped.
|
|
89
|
+
def running_anycable_pid
|
|
90
|
+
pid = File.read(pidfile).to_i if File.exist?(pidfile)
|
|
91
|
+
return unless pid&.positive?
|
|
92
|
+
return unless anycable_process?(pid)
|
|
93
|
+
|
|
94
|
+
# After a restart (re-exec), the server is still our child process
|
|
95
|
+
if Process.waitpid(pid, Process::WNOHANG).nil?
|
|
96
|
+
log "* Using the running AnyCable server (pid: #{pid})"
|
|
97
|
+
pid
|
|
98
|
+
end
|
|
99
|
+
rescue Errno::ECHILD
|
|
100
|
+
log "* Stopping the orphaned AnyCable server (pid: #{pid})..."
|
|
101
|
+
terminate_group(pid)
|
|
102
|
+
nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def anycable_process?(pid)
|
|
106
|
+
Process.getpgid(pid) == pid &&
|
|
107
|
+
IO.popen(["ps", "-o", "command=", "-p", pid.to_s], &:read).include?(File.basename(command.first))
|
|
108
|
+
rescue Errno::ESRCH, Errno::EPERM
|
|
109
|
+
false
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def monitor_anycable(waiter)
|
|
113
|
+
status = waiter.value
|
|
114
|
+
return if @stopping
|
|
115
|
+
|
|
116
|
+
FileUtils.rm_f(pidfile)
|
|
117
|
+
log "! AnyCable server has exited (#{status})"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def stop_anycable
|
|
121
|
+
return unless anycable_pid
|
|
122
|
+
|
|
123
|
+
@stopping = true
|
|
124
|
+
log "* Stopping AnyCable server..."
|
|
125
|
+
terminate_group(anycable_pid)
|
|
126
|
+
FileUtils.rm_f(pidfile)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def terminate_group(pid)
|
|
130
|
+
Process.kill(:TERM, -pid)
|
|
131
|
+
Process.kill(:KILL, -pid) unless group_stopped?(pid)
|
|
132
|
+
rescue Errno::ESRCH
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def group_stopped?(pid)
|
|
136
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + stop_timeout
|
|
137
|
+
|
|
138
|
+
while Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
|
|
139
|
+
Process.kill(0, -pid)
|
|
140
|
+
sleep 0.1
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
false
|
|
144
|
+
rescue Errno::ESRCH
|
|
145
|
+
true
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def command
|
|
149
|
+
@command ||= Shellwords.split(options.fetch(:anycable_command, "bin/anycable-go"))
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def pidfile
|
|
153
|
+
options.fetch(:anycable_pidfile, "tmp/pids/anycable.pid")
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def output_options
|
|
157
|
+
path = options[:anycable_log]
|
|
158
|
+
return {} unless path
|
|
159
|
+
|
|
160
|
+
{out: [path, "a"], err: [:child, :out]}
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def stop_timeout
|
|
164
|
+
10
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def log(msg)
|
|
168
|
+
log_writer.log(msg)
|
|
169
|
+
rescue Errno::EIO, Errno::EPIPE, Errno::EBADF
|
|
170
|
+
end
|
|
171
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: anycable-rails
|
|
3
|
+
description: Install, configure, deploy, debug and build real-time features with AnyCable in a Ruby on Rails app. Use when the user wants to add AnyCable or replace Action Cable, set up WebSockets for Hotwire/Turbo Streams, or deploy AnyCable (Thruster, Kamal, Docker, Heroku, Fly, AnyCable+). Also use when WebSocket connections fail, broadcasts do not arrive, subscriptions are rejected, clients reconnect in a loop, or a deploy causes a reconnection spike/storm. Also use when you build any live feature (chat, notifications, live updates, typing indicators, cursors, presence/online lists, live dashboards, `turbo_stream_from`, `broadcast_*_to`, Action Cable channels) in an app that uses or may use AnyCable.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# AnyCable on Rails
|
|
7
|
+
|
|
8
|
+
AnyCable is a real-time server that replaces the Action Cable server in a Rails app.
|
|
9
|
+
The `anycable-rails` gem connects Rails to the AnyCable server (`anycable-go`).
|
|
10
|
+
Channels, `turbo_stream_from` and `broadcast_*_to` keep working.
|
|
11
|
+
|
|
12
|
+
Pick the task, then read only the reference files that the task needs.
|
|
13
|
+
|
|
14
|
+
| Task | Read |
|
|
15
|
+
|---|---|
|
|
16
|
+
| Add AnyCable to an app, change the setup | `references/installation.md` |
|
|
17
|
+
| Put AnyCable in production | `references/deployment.md` |
|
|
18
|
+
| Run system tests against AnyCable | `references/testing.md` |
|
|
19
|
+
| Something does not work | `references/troubleshooting.md`, then `references/live-checks.md` |
|
|
20
|
+
| Build a real-time feature | `references/realtime-features.md` |
|
|
21
|
+
| Turbo Streams, Hotwire JS setup, broadcasts to others, the presence element | `references/hotwire.md` |
|
|
22
|
+
| Who is online, presence lists, presence from Rails | `references/presence.md` |
|
|
23
|
+
| Show different HTML to different users in a broadcast | `references/personalization.md` |
|
|
24
|
+
|
|
25
|
+
Documentation: <https://docs.anycable.io>. Use the docs for details. The reference files give the decisions and the short path.
|
|
26
|
+
|
|
27
|
+
## 1. Find out what the app has
|
|
28
|
+
|
|
29
|
+
Do this before you change anything:
|
|
30
|
+
|
|
31
|
+
1. Check `Gemfile.lock` for `anycable-rails`, `anycable-rails-core`, `anycable-thruster`, `turbo-rails`, `redis`, `nats-pure`.
|
|
32
|
+
2. Read `config/cable.yml`, `config/anycable.yml` and `anycable.toml` (if they exist).
|
|
33
|
+
3. List `app/channels/**/*_channel.rb`. Custom channels need RPC. Turbo-only apps do not.
|
|
34
|
+
4. Read `app/channels/application_cable/connection.rb`. Note the `identified_by` names and how `connect` finds the user.
|
|
35
|
+
5. Find the JS entry point. Note `@rails/actioncable`, `@hotwired/turbo-rails`, `@anycable/web`, `@anycable/turbo-stream`.
|
|
36
|
+
6. Check the deployment files: `Dockerfile`, `config/deploy.yml` (Kamal), `Procfile`, `fly.toml`, `render.yaml`.
|
|
37
|
+
|
|
38
|
+
## 2. Default recommendations
|
|
39
|
+
|
|
40
|
+
Use these defaults unless the app has a reason to do otherwise. Tell the user when you deviate.
|
|
41
|
+
|
|
42
|
+
- **Use the generator.** Run `bin/rails g anycable:setup`, then tune its output. Do not write the config by hand.
|
|
43
|
+
- **Ask how to run the server in development.** Recommend `Procfile.dev`, the Puma plugin, or a Docker service by the app setup (`references/installation.md` §8).
|
|
44
|
+
- **Avoid RPC when the app allows it.** Use JWT authentication and signed streams. Without RPC, the AnyCable server does not call Rails on connect or subscribe. That is faster and safer under load.
|
|
45
|
+
- **Use the AnyCable JS client** (`@anycable/web`) with the extended protocol `actioncable-v1-ext-json`. It reconnects with backoff, restores sessions, and supports reliable streams and presence.
|
|
46
|
+
- **With Hotwire, use `@anycable/turbo-stream`**, not `@hotwired/turbo-rails`. The `turbo-rails` package creates its own Action Cable consumer.
|
|
47
|
+
- **Use the `http` broadcast adapter** as the start. Switch to `redisx` or NATS only for a reason (see `references/installation.md`).
|
|
48
|
+
- **Keep one cable instance per page.** Create it in one module and import it everywhere.
|
|
49
|
+
|
|
50
|
+
## 3. Real-time feature checklist
|
|
51
|
+
|
|
52
|
+
Use this list every time you add a live feature:
|
|
53
|
+
|
|
54
|
+
1. Choose the transport: a signed stream (Turbo or `$pubsub`) or a channel. Use a channel only for client-to-server actions or server-side logic per subscription. See `references/realtime-features.md` §1.
|
|
55
|
+
2. Make broadcasts context-free. A broadcast has no `current_user`. Send the same payload to all subscribers and personalize on the client. See `references/personalization.md`.
|
|
56
|
+
3. Make updates idempotent. A client can get a message twice. Use stable DOM ids and `replace`/`update`, or check for duplicates.
|
|
57
|
+
4. Close the page-load gap with reliable streams and the history timestamp meta tag. See `references/realtime-features.md` §6.
|
|
58
|
+
5. Render broadcasts in jobs (`broadcast_*_later_to`) when the HTML is not trivial.
|
|
59
|
+
6. For ephemeral UI state (typing, cursors) use whispers. For "who is online" use presence. Do not store either in the database.
|
|
60
|
+
7. Test the feature: a broadcast assertion in a unit test, plus a system test against a real AnyCable server for the main flow (`references/testing.md`).
|
|
61
|
+
|
|
62
|
+
## 4. Do not
|
|
63
|
+
|
|
64
|
+
- Do not mount `ActionCable.server` in `routes.rb` when AnyCable serves WebSockets.
|
|
65
|
+
- Do not use instance variables to keep state between channel actions. Use `state_attr_accessor` or `params`. Channel objects do not live between calls.
|
|
66
|
+
- Do not use `periodically` timers or `stream_from` with a block. AnyCable does not support them.
|
|
67
|
+
- Do not put logic that must run in `Connection#connect` when you use JWT. With JWT, AnyCable does not call `connect`.
|
|
68
|
+
- Do not rely on `disconnect` or `unsubscribed` callbacks for business-critical state, for example an `online` column. AnyCable can skip or delay these calls. Use presence.
|
|
69
|
+
- Do not loop over users to broadcast to each user's stream. Broadcast once to a shared stream.
|
|
70
|
+
- Do not import `@hotwired/turbo-rails` and `@anycable/turbo-stream` together.
|
|
71
|
+
- Do not put secrets into code or `anycable.toml` for production. Use env vars or credentials.
|
|
72
|
+
- Do not suggest AnyCable Thruster for development, unless the app already uses it there.
|
|
73
|
+
|
|
74
|
+
## 5. Report
|
|
75
|
+
|
|
76
|
+
When you finish, tell the user:
|
|
77
|
+
|
|
78
|
+
- the RPC mode (none, HTTP, gRPC) and why;
|
|
79
|
+
- the authentication method (JWT or cookies);
|
|
80
|
+
- the broadcast adapter and the broker settings;
|
|
81
|
+
- the env vars that production needs (names only, never values);
|
|
82
|
+
- what you checked live (wsdirector, browser) and what you did not check.
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# Deploying AnyCable
|
|
2
|
+
|
|
3
|
+
Summary: pick the option that matches the app's platform. For one server, use AnyCable Thruster.
|
|
4
|
+
For a managed server, use AnyCable+. Run the WebSocket server separately from Rails when you need deploys with no disconnects.
|
|
5
|
+
|
|
6
|
+
Docs: <https://docs.anycable.io/deployment>.
|
|
7
|
+
|
|
8
|
+
## 1. Choose an option
|
|
9
|
+
|
|
10
|
+
| Option | Use it when | WebSocket URL | RPC |
|
|
11
|
+
|---|---|---|---|
|
|
12
|
+
| AnyCable Thruster | Rails 8 Dockerfile with Thruster, one server (Kamal single host) | Same host, `/cable` | none or HTTP |
|
|
13
|
+
| AnyCable+ (<https://plus.anycable.io>) | You do not want to run the server yourself | Managed host, for example `wss://<name>.fly.dev/cable` | none or HTTP |
|
|
14
|
+
| Separate server (Docker image, Kamal accessory) | Many clients, several app servers, deploys with no disconnects | Own subdomain, for example `wss://ws.example.com/cable` | any |
|
|
15
|
+
| Heroku | The app runs on Heroku | Separate Heroku app | HTTP (simple) or gRPC (buildpack) |
|
|
16
|
+
| Fly.io | The app runs on Fly | Separate Fly app | gRPC or HTTP |
|
|
17
|
+
|
|
18
|
+
A separate WebSocket domain means cookies do not reach the server unless the domains share a parent. Use JWT with any separate host.
|
|
19
|
+
|
|
20
|
+
## 2. Production checklist
|
|
21
|
+
|
|
22
|
+
For every option:
|
|
23
|
+
|
|
24
|
+
1. Set the same `ANYCABLE_SECRET` on the Rails side and the server side.
|
|
25
|
+
2. Set `ANYCABLE_WEBSOCKET_URL` on the Rails side (`wss://...`).
|
|
26
|
+
3. Set the broadcast target on the Rails side: `ANYCABLE_HTTP_BROADCAST_URL` (for `http`) or `REDIS_URL` / `ANYCABLE_REDIS_URL` (for `redisx`).
|
|
27
|
+
4. Set the RPC target on the server side: `ANYCABLE_RPC_HOST`, or `ANYCABLE_NORPC=true`.
|
|
28
|
+
5. Hotwire apps: `ANYCABLE_TURBO_STREAMS=true` on the server (or `[streams] turbo = true`).
|
|
29
|
+
6. Set `ANYCABLE_ALLOWED_ORIGINS` on the server (for example `example.com,*.example.com`).
|
|
30
|
+
7. With gRPC: keep the database pool larger than the gRPC worker pool (`rpc_pool_size`, default 30).
|
|
31
|
+
8. Add the WebSocket origin to the CSP `connect_src`.
|
|
32
|
+
9. Read the connection avalanche section in `troubleshooting.md` §5 before the first production deploy with many clients.
|
|
33
|
+
|
|
34
|
+
Server env vars: <https://docs.anycable.io/anycable-go/configuration>. Rails env vars: <https://docs.anycable.io/ruby/configuration>.
|
|
35
|
+
|
|
36
|
+
## 3. AnyCable Thruster
|
|
37
|
+
|
|
38
|
+
AnyCable Thruster is a fork of the Thruster proxy by Basecamp with the AnyCable server inside. It serves WebSockets on `/cable` of the app host.
|
|
39
|
+
|
|
40
|
+
1. Replace the gem: `bundle remove thruster && bundle add anycable-thruster`.
|
|
41
|
+
2. Keep the Dockerfile command: `CMD ["./bin/thrust", "./bin/rails", "server"]`.
|
|
42
|
+
3. Configure the server with `ANYCABLE_*` env vars, or with `ANYCABLE_OPT="--flag=value ..."`. Do not use `anycable.toml` in production:
|
|
43
|
+
- AnyCable Thruster reads `anycable.toml` from the working directory (`/rails` in the image).
|
|
44
|
+
- The development file has a development secret and `debug = true`.
|
|
45
|
+
- Add `/anycable.toml` to `.dockerignore`.
|
|
46
|
+
4. Choose the RPC mode:
|
|
47
|
+
- none: `ANYCABLE_NORPC=true`;
|
|
48
|
+
- HTTP: `ANYCABLE_RPC_HOST=http://localhost:3000/_anycable` (the Rails port inside the container) and `http_rpc_mount_path: /_anycable`.
|
|
49
|
+
5. Set `ANYCABLE_WEBSOCKET_URL=wss://app.example.com/cable`.
|
|
50
|
+
6. Broadcasts: the defaults of the server and Rails match. No settings are necessary:
|
|
51
|
+
- with a secret, the server accepts broadcasts on its main port (8080), and Rails sends them to `http://localhost:8080/_broadcast`;
|
|
52
|
+
- without a secret, both use port 8090.
|
|
53
|
+
|
|
54
|
+
Set `ANYCABLE_HTTP_BROADCAST_URL` only when you change `ANYCABLE_PORT` or `ANYCABLE_HTTP_BROADCAST_PORT`. Use the same port in the URL. Sources: `cli/options.go` (anycable-go), `Config#http_broadcast_url` (the `anycable-core` gem).
|
|
55
|
+
|
|
56
|
+
Trade-off: every app deploy restarts the WebSocket server. All clients reconnect.
|
|
57
|
+
|
|
58
|
+
Repository: <https://github.com/anycable/thruster>.
|
|
59
|
+
|
|
60
|
+
## 4. AnyCable+
|
|
61
|
+
|
|
62
|
+
AnyCable+ runs the server for you. Manage cables in the dashboard or with the `anycable-plus` CLI.
|
|
63
|
+
|
|
64
|
+
### The `anycable-plus` CLI
|
|
65
|
+
|
|
66
|
+
If the user chooses AnyCable+, suggest the CLI. It creates cables and prints their settings in the terminal.
|
|
67
|
+
|
|
68
|
+
1. Install the CLI. Then restart the shell.
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
curl -LSs https://anycable-plus.terminalwire.sh/stable | bash
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
2. Log in. The command opens the browser. The user must approve the login there.
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
anycable-plus login
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
3. Create a cable. Pass the app secret, so that JWT and signed streams work:
|
|
81
|
+
|
|
82
|
+
```sh
|
|
83
|
+
anycable-plus cable create my-app --framework=hotwire --secret="$ANYCABLE_SECRET" --wait
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
| Option | Meaning |
|
|
87
|
+
|---|---|
|
|
88
|
+
| `--framework` | `default`, `rails`, `hotwire`, `js`, `laravel`. Fills in the configuration for the framework. |
|
|
89
|
+
| `--secret` | The AnyCable secret. Use the same value as on the Rails side. |
|
|
90
|
+
| `--rpc_host` | The HTTP RPC URL, for example `https://app.example.com/_anycable`. Only for apps with channels. |
|
|
91
|
+
| `--public` | Public mode: no secret. |
|
|
92
|
+
| `--wait` | Wait until the cable is ready, then print its settings. |
|
|
93
|
+
|
|
94
|
+
4. Print the settings at any time: `anycable-plus cable info <id>`. Get the id from `anycable-plus cables`.
|
|
95
|
+
|
|
96
|
+
Other commands: `anycable-plus cable update <id> --secret=... --rpc_host=...`, `anycable-plus cable destroy <id>`, `anycable-plus whoami`.
|
|
97
|
+
|
|
98
|
+
Run `anycable-plus help` for the full list. CLI page: <https://anycable-plus.terminalwire.sh>.
|
|
99
|
+
|
|
100
|
+
### Settings
|
|
101
|
+
|
|
102
|
+
`cable info` and the dashboard give the values for each setting:
|
|
103
|
+
|
|
104
|
+
| Value | Rails setting |
|
|
105
|
+
|---|---|
|
|
106
|
+
| WebSocket URL | `ANYCABLE_WEBSOCKET_URL` |
|
|
107
|
+
| Secret | `ANYCABLE_SECRET` |
|
|
108
|
+
| Broadcast URL | `ANYCABLE_HTTP_BROADCAST_URL` |
|
|
109
|
+
| Broadcast key | `ANYCABLE_BROADCAST_KEY` |
|
|
110
|
+
| RPC URL (you enter it) | Your app's HTTP RPC endpoint, for example `https://app.example.com/_anycable` |
|
|
111
|
+
| RPC secret | `ANYCABLE_HTTP_RPC_SECRET` |
|
|
112
|
+
|
|
113
|
+
Use `broadcast_adapter: http`. Use HTTP RPC or no RPC. gRPC is not available.
|
|
114
|
+
|
|
115
|
+
## 5. Separate server: Docker and Kamal
|
|
116
|
+
|
|
117
|
+
- Image: `anycable/anycable-go:1.6` (Pro: `ghcr.io/anycable/anycable-go-pro`).
|
|
118
|
+
- Set `ANYCABLE_HOST=0.0.0.0` in containers.
|
|
119
|
+
- Health check path: `/health`.
|
|
120
|
+
|
|
121
|
+
Kamal accessory (from <https://docs.anycable.io/deployment/kamal>):
|
|
122
|
+
|
|
123
|
+
```yaml
|
|
124
|
+
accessories:
|
|
125
|
+
anycable-go:
|
|
126
|
+
image: anycable/anycable-go:1.6
|
|
127
|
+
host: 192.168.0.1
|
|
128
|
+
proxy:
|
|
129
|
+
host: ws.example.com
|
|
130
|
+
ssl: true
|
|
131
|
+
app_port: 8080
|
|
132
|
+
healthcheck:
|
|
133
|
+
path: /health
|
|
134
|
+
env:
|
|
135
|
+
clear:
|
|
136
|
+
ANYCABLE_HOST: "0.0.0.0"
|
|
137
|
+
ANYCABLE_PORT: 8080
|
|
138
|
+
ANYCABLE_BROADCAST_ADAPTER: http
|
|
139
|
+
ANYCABLE_HTTP_BROADCAST_PORT: 8080
|
|
140
|
+
secret:
|
|
141
|
+
- ANYCABLE_SECRET
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
- The server accepts broadcasts on the proxied port. The secret protects the endpoint. Set `ANYCABLE_HTTP_BROADCAST_URL=https://ws.example.com/_broadcast` on the Rails side.
|
|
145
|
+
- Add the RPC setting to `env.clear`: `ANYCABLE_NORPC: true`, an HTTP RPC URL, or the gRPC role address.
|
|
146
|
+
- The gRPC server runs as a separate role: `cmd: bundle exec anycable`, `proxy: false`.
|
|
147
|
+
- Kamal Proxy does not proxy gRPC. Connect over the Docker network alias.
|
|
148
|
+
- Read the Kamal guide for the rolling deploy hook of the RPC role.
|
|
149
|
+
|
|
150
|
+
Docs: <https://docs.anycable.io/deployment/docker>, <https://docs.anycable.io/deployment/kamal>, <https://docs.anycable.io/deployment/kubernetes>.
|
|
151
|
+
|
|
152
|
+
## 6. Heroku
|
|
153
|
+
|
|
154
|
+
- Simple path: HTTP RPC. Deploy the server as a second Heroku app (the deploy button or the Docker image).
|
|
155
|
+
- Server app env: `ANYCABLE_RPC_HOST=https://<app>.herokuapp.com/_anycable`, `ANYCABLE_SECRET`.
|
|
156
|
+
- Turn on Dyno Metadata, or set `ANYCABLE_HOST=0.0.0.0`.
|
|
157
|
+
- Standard dynos limit open files to 10 000. That limits connections per dyno.
|
|
158
|
+
- gRPC on Heroku needs the `heroku-anycable-go` buildpack and a second app. Read the guide.
|
|
159
|
+
|
|
160
|
+
Docs: <https://docs.anycable.io/deployment/heroku>.
|
|
161
|
+
|
|
162
|
+
## 7. Fly.io
|
|
163
|
+
|
|
164
|
+
- Run the server as a separate Fly app. Use the `.internal` network for RPC and broadcasts.
|
|
165
|
+
- `ANYCABLE_FLY_WS_APP_NAME` (Rails side) and `ANYCABLE_FLY_RPC_APP_NAME` (server side) let presets build the internal addresses.
|
|
166
|
+
- Prefer `ANYCABLE_WEBSOCKET_URL` over custom env vars for the public URL.
|
|
167
|
+
|
|
168
|
+
Docs: <https://docs.anycable.io/deployment/fly>.
|
|
169
|
+
|
|
170
|
+
## 8. AnyCable Pro
|
|
171
|
+
|
|
172
|
+
Suggest Pro when the app needs one of these:
|
|
173
|
+
|
|
174
|
+
- a shared broker for reliable streams and presence on several server nodes (Redis broker);
|
|
175
|
+
- adaptive RPC concurrency (`--rpc_concurrency=0`);
|
|
176
|
+
- slow drain on shutdown (`ANYCABLE_SHUTDOWN_SLOWDRAIN=true`);
|
|
177
|
+
- binary formats (Msgpack, Protobuf), long polling, less memory per connection.
|
|
178
|
+
|
|
179
|
+
Docs: <https://docs.anycable.io/pro>.
|
|
180
|
+
|
|
181
|
+
## 9. Load balancers and proxies
|
|
182
|
+
|
|
183
|
+
- Use the "least connections" strategy for WebSocket servers.
|
|
184
|
+
- Allow long-lived connections: raise the proxy idle timeout above the ping interval (3 s by default).
|
|
185
|
+
- NGINX needs `proxy_set_header Upgrade` and `Connection "upgrade"`.
|
|
186
|
+
|
|
187
|
+
Docs: <https://docs.anycable.io/deployment/load_balancing>.
|