Smeltery
Get started

Anvil (broadcasting)

v0.1

Settings and limits

On this page
Key Default Meaning
ANVIL_APP_ID smeltery the app id
ANVIL_APP_KEY derived from APP_KEY (20 characters) the public key in the socket path; letters, digits, -, _, at most 64. The derived key changes when APP_KEY changes: set it in .env when clients are built with it (a front end bundle, a mobile app)
ANVIL_APP_SECRET derived from APP_KEY the secret signatures are made with; set it only when another deployment must make them; at least 32 bytes outside APP_ENV=local / testing, else the app stops at boot
ANVIL_ALLOWED_ORIGINS empty origins allowed besides APP_URL's, comma-separated; * allows any (logged as a warning outside APP_ENV=local)
ANVIL_MAX_CONNECTIONS 2048 sockets in this process (below SERVER_MAX_CONNECTIONS); past it a socket is closed with 4100
ANVIL_MAX_CONNECTIONS_PER_IP 100 sockets of one client address (an IPv6 client by its /64, the address from TRUSTED_PROXIES resolution); past it 4100; below SERVER_MAX_CONNECTIONS_PER_IP when both are on, else the app stops at boot; 0 turns it off and leaves only the server's per-address cap
ANVIL_HANDSHAKES_PER_MINUTE 60 socket handshakes of one client address a minute; past it 429. Once 100,000 addresses are counted in a minute, further ones are counted with their network (an IPv4 /16, an IPv6 /48) at 16 times the budget, and once 10,000 networks are counted, further clients are limited only by the socket caps
ANVIL_MAX_SUBSCRIPTIONS 100 channels per socket; past it the subscription gets status 429
ANVIL_MAX_MESSAGE_SIZE 10000 bytes of one message from a client; a larger one closes the socket with 1009
ANVIL_MAX_EVENT_SIZE 32768 bytes of one event's data (at most 49152)
ANVIL_MAX_PRESENCE_MEMBERS 100 distinct users in one presence channel (1 to 10000); the member list holds at most this many
ANVIL_MAX_MEMBER_BYTES 1024 bytes of one member's channel_data (64 to 8192)
ANVIL_MAX_PRESENCE_CHANNELS 10 presence channels one socket is in (1 to 100)
ANVIL_ACTIVITY_TIMEOUT 30 seconds (1 to 3600); clients ping after this much silence
ANVIL_PING_INTERVAL 60 seconds of silence (1 to 3600) before the server sends pusher:ping
ANVIL_PONG_TIMEOUT 30 seconds (1 to 300) to answer it; then the socket is closed with 4201
ANVIL_MAX_CONNECTION_AGE 86400 seconds (60 to 2592000) a socket lives; then it is closed with 4200 and the client reconnects
ANVIL_IN_SERVE true false: serve answers the socket path with 404, because an anvil process holds the sockets (see A separate socket process)
ANVIL_SERVER_HOST 127.0.0.1 the address the anvil process listens on
ANVIL_SERVER_PORT 8080 the port the anvil process listens on

A timer value outside its range in .env is replaced by the nearest bound, with a warning; outside its range in Settings passed to anvil_with, it stops the app at boot.

A socket may send 20 frames a second with bursts of 40; a frame over that is dropped with one pusher:error 4301, and 40 more in a row close the socket with 4100. Each socket has an outbox of 256 messages; a socket that does not read them is closed with 4100, and a write that takes longer than 10 seconds drops it. A binary frame closes it with 1003, the server's shutdown with 1001. pusher-js reconnects after codes below 4000 and from 4100 to 4299, and stops after 4007, 4008 (protocol version unsupported or missing) and 4009.

A socket keeps its connection's place in SERVER_MAX_CONNECTIONS and SERVER_MAX_CONNECTIONS_PER_IP, so sockets and HTTP requests share those limits, and the server's idle timeout does not close it. Sockets are closed with 1001 when the app shuts down, within SHUTDOWN_TIMEOUT. Frames are never logged.

Behind a proxy#

Caddy's reverse_proxy passes WebSocket upgrades as it is. nginx forwards Upgrade and Connection only when told to; add this location to the server block (with the app's key):

nginx
location = /app/ANVIL_APP_KEY_VALUE {
    proxy_pass http://127.0.0.1:8000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    # Longer than ANVIL_PING_INTERVAL + ANVIL_PONG_TIMEOUT.
    proxy_read_timeout 120s;
    proxy_send_timeout 120s;
}

Behind a proxy every socket comes from the proxy's address; with the proxy in TRUSTED_PROXIES, the per-client limits count the client from X-Forwarded-For.

A separate socket process#

serve holds the sockets by default. The anvil command runs the app binary as a process that serves only the socket endpoint (/app/<ANVIL_APP_KEY>) and /up, on ANVIL_SERVER_HOST:ANVIL_SERVER_PORT (127.0.0.1:8080; --host and --port override them); every other path answers 404. The web processes can then restart (a deploy) without closing a socket.

shell
smeltery anvil              # in development
/srv/myapp/myapp anvil      # the release binary
  • The web processes run with ANVIL_IN_SERVE=false: their socket path answers 404. They keep the auth endpoints (/broadcasting/auth, /api/broadcasting/auth), which need the session and the guards; the anvil process checks the signatures they make with the same secret (derived from the same APP_KEY, or the same ANVIL_APP_SECRET) and the same ANVIL_APP_KEY.
  • Events, revocations and presence reach the anvil process through the PubSub. Under PUBSUB_DRIVER=auto the anvil process and a serve with ANVIL_IN_SERVE=false use the shared driver (redis when CACHE_STORE=redis, else database); without a usable shared driver anvil stops at start with an error. Console commands use local under auto, so a deployment whose commands send events sets PUBSUB_DRIVER=database or redis. The database driver needs the pubsub_messages table, presence the presence tables (see Presence channels).
  • No agents, jobs or schedules run in it; serve or work runs them.
  • The server's limits hold on its listener as on serve's (SERVER_MAX_CONNECTIONS, SERVER_MAX_CONNECTIONS_PER_IP, SERVER_HEADER_TIMEOUT, TRUSTED_PROXIES), with the ANVIL_* limits above. On SIGTERM it closes its sockets with 1001 and exits within SHUTDOWN_TIMEOUT; its presence members leave with them.
  • With ANVIL_IN_SERVE left at true it logs a warning at start: serve processes hold sockets too.

systemd. A third unit next to the one of systemd (or next to myapp-web and myapp-work): set ANVIL_IN_SERVE=false in .env, then

shell
sudo cp /etc/systemd/system/myapp.service /etc/systemd/system/myapp-anvil.service
sudo nano /etc/systemd/system/myapp-anvil.service

and change Description=myapp sockets (Smeltery) and ExecStart=/srv/myapp/myapp anvil. Each socket holds a file descriptor: for more than about a thousand sockets add LimitNOFILE=65536 under [Service] and raise SERVER_MAX_CONNECTIONS (4096) and ANVIL_MAX_CONNECTIONS (2048, below it) in .env.

shell
sudo systemctl daemon-reload
sudo systemctl enable --now myapp-anvil
sudo systemctl restart myapp
curl -s http://127.0.0.1:8080/up

Caddy sends the socket path to the anvil process and everything else to the web process:

text
app.example.com {
    handle /app/* {
        reverse_proxy 127.0.0.1:8080
    }
    handle {
        reverse_proxy 127.0.0.1:8000
    }
}

nginx: the location = /app/ANVIL_APP_KEY_VALUE block above with proxy_pass http://127.0.0.1:8080;.

↑↓ moveEnter openEsc close