Skip to content
cabby

Scale across nodes with NATS

NATS can do three jobs for a group of Cabby nodes, and you pick each one separately: pub/sub between the nodes, a broadcast adapter for your app, and, with JetStream, a broker that keeps stream history and sessions for all of them. All three connect to the servers in --nats_servers, which defaults to nats://127.0.0.1:4222.

If you already run Redis, the Redis guide does the same with it. If you already run PostgreSQL, the PostgreSQL guide does the same with it.

Pub/sub between nodes

cabby --rpc_host=http://localhost:3000/_anycable --secret=change-me --broadcast_adapter=http \
  --pubsub=nats --nats_servers=nats://nats.internal:4222

With --pubsub=nats, a broadcast received by one node reaches the clients of every node. Your app keeps broadcasting over HTTP to any one node, and that node passes the message on through NATS. For a NATS cluster, list its servers in --nats_servers, separated by commas.

At startup, the log has a line that starts with Starting NATS pub/sub: and names the servers.

Broadcasting through NATS

Your app can also hand its broadcasts to NATS instead of sending them to Cabby over HTTP. Add nats to the broadcast adapters:

cabby --rpc_host=http://localhost:3000/_anycable --secret=change-me \
  --broadcast_adapter=http,nats --nats_servers=nats://nats.internal:4222

Every node subscribes to the NATS subject __anycable__, which you can change with --nats_channel. Each node receives every broadcast and delivers it to its own clients, so this adapter reaches all the nodes on its own. Point your app's NATS broadcast adapter at the same servers and the same channel.

These broadcasts are delivered live and aren't stored in history, so a client that reconnects later can't catch up on them. Broadcast over HTTP when you want history. If you list nats next to a broker that keeps history, Cabby says so at startup:

WRN broadcasts through the nats adapter are delivered live and not stored in history; use http or redisx to keep history

Shared history and sessions with JetStream

--broker=nats keeps stream history and sessions in NATS JetStream, so every node shares them. A client that reconnects to any node catches up on what it missed and gets its session back.

cabby --rpc_host=http://localhost:3000/_anycable --secret=change-me --broadcast_adapter=http \
  --broker=nats --pubsub=nats --nats_servers=nats://nats.internal:4222
  • The NATS server needs JetStream, which nats-server -js turns on. If Cabby doesn't find it, it retries a few times and then stops with JetStream is unavailable in the error.
  • --broker=nats doesn't pick a pub/sub adapter. Keep --pubsub=nats, so live messages reach every node.
  • History is kept within --history_limit and --history_ttl, and sessions within --sessions_ttl, as with the other brokers. Configuration lists them.
  • All the nodes share one epoch, which is stored in NATS. If it changes, each node logs epoch updated, and reconnecting clients resync cleanly.
  • There's no presence with this broker. If you need presence, use the in-memory broker on a single node.

In NATS, each stream gets a JetStream stream named _ac_<stream>. Sessions go to the key-value bucket _anycable_, and the epoch to _anycable_epoch_.

The startup log has Using NATS broker: with the servers and the limits, then NATS broker is ready with the epoch.

Your own NATS server, or an embedded one

Everything above works with a NATS server you run yourself, for example the official nats Docker image. For a new setup, go with that.

--embed_nats is for setups that already rely on it. It starts a nats-server as a child process of Cabby and points --nats_servers at it, unless you set that yourself. You still choose what uses it: --pubsub=nats, --broadcast_adapter=nats, --broker=nats. With --presets=broker, --embed_nats picks all three for you, next to HTTP broadcasts.

  • The downloads and the Docker image don't include nats-server. Put it next to cabby or on your PATH, or set CABBY_NATS_SERVER_BIN to its location.
  • The embedded server listens on nats://127.0.0.1:4222 unless --enats_addr says otherwise. To join the embedded servers of several nodes into one cluster, give each a cluster address with --enats_cluster and the addresses of the others with --enats_cluster_routes.
  • With --broker=nats, the embedded server runs with JetStream. Its data goes under nats-data in the system's temporary directory; set --enats_store_dir to keep it somewhere durable.
  • If the nats-server process exits, Cabby logs embedded NATS server exited unexpectedly and stops as well.

Two nodes, one NATS server

Here's the whole thing on one machine: a NATS server with JetStream, and two Cabby nodes that share pub/sub, history and sessions through it.

docker run -d --name nats -p 4222:4222 nats -js

cabby --port=8080 --rpc_host=http://localhost:3000/_anycable --secret=change-me \
  --broadcast_adapter=http --pubsub=nats --broker=nats --nats_servers=nats://localhost:4222

cabby --port=8081 --rpc_host=http://localhost:3000/_anycable --secret=change-me \
  --broadcast_adapter=http --pubsub=nats --broker=nats --nats_servers=nats://localhost:4222

Connect a client to each port and broadcast to either node: both clients get the message. Move a client from one node to the other, and it catches up from the shared history.

In production, the nodes sit behind your load balancer, and your app sends each broadcast to any one of them.