github.com/anycable/anycable-go@v1.5.1/docs/pubsub.md (about) 1 # Pub/Sub for node-node communication 2 3 When running multiple instances of AnyCable-Go, you have two options to deliver broadcast messages to all nodes (and, thus, clients connected to each node). The first, legacy option is to use a fan-out, or distributed broadcasting adapter (Redis or NATS), i.e., deliver messages to all nodes simultaneously and independently. The second option is to publish a message to a single node (picked randomly), and then let AnyCable-Go to re-transmit it to other nodes from the cluster. The latter is **required** for multi-node setups with a [broker](./broker.md). 4 5 Although, we do not plan to sunset legacy, distributed adapters in the nearest future, we recommend switching to a new broadcaster+pubsub (_B+P/S_) architecture for the following reasons: 6 7 - Broker (aka _streams history_) requires the new architecture. 8 - The new architecture scales better by avoiding sending **all** messages to **all** nodes; only the nodes _interested_ in a particular stream (i.e., having active subscribers) receive broadcast messages via pub/sub. 9 10 **NOTE:** The new architecture will be the default one since v1.5. 11 12 ## Usage 13 14 By default, pub/sub is disabled (since the default broadcast adapter is legacy, fan-out Redis). To enable the pub/sub layer, you must provide the name of the provider via the `--pubsub` option. 15 16 You also need to enable a compatible broadcasting adapter. See [broadcasting](./broadcasting.md). 17 18 **NOTE**: It's safe to enable `--pubsub` even if you're still using legacy broadcasting adapters (they do not pass messages through the pub/sub layer). 19 20 ## Supported adapters 21 22 ### Redis 23 24 The Redis pub/sub adapter uses the Publish/Subscribe Redis feature to re-transmit messages within a cluster. To enable it, set the value of the`pubsub` parameter to `redis`: 25 26 ```sh 27 $ anycable-go --pubsub=redis 28 # or 29 $ ANYCABLE_PUBSUB=redis anycable-go 30 31 INFO 2023-04-18T20:46:00.692Z context=main Starting AnyCable 1.4.0-36a43e5 (with mruby 1.2.0 (2015-11-17)) (pid: 16574, open file limit: 122880, gomaxprocs: 8) 32 INFO 2023-04-18T20:46:00.693Z context=pubsub Starting Redis pub/sub: localhost:6379 33 ... 34 ``` 35 36 See [configuration](./configuration.md) for available Redis configuration settings. 37 38 ### NATS 39 40 ```sh 41 $ anycable-go --pubsub=nats 42 # or 43 $ ANYCABLE_PUBSUB=nats anycable-go 44 45 INFO 2023-04-18T20:28:38.410Z context=main Starting AnyCable 1.4.0-36a43e5 (with mruby 1.2.0 (2015-11-17)) (pid: 9125, open file limit: 122880, gomaxprocs: 8) 46 INFO 2023-04-18T20:28:38.411Z context=pubsub Starting NATS pub/sub: nats://127.0.0.1:4222 47 ... 48 ``` 49 50 You can use it with the [embedded NATS](./embedded_nats.md), too: 51 52 ```sh 53 $ anycable-go --embed_nats --pubsub=nats 54 55 INFO 2023-04-18T20:30:58.724Z context=main Starting AnyCable 1.4.0-36a43e5 (with mruby 1.2.0 (2015-11-17)) (pid: 9615, open file limit: 122880, gomaxprocs: 8) 56 INFO 2023-04-18T20:30:58.753Z context=main Embedded NATS server started: nats://127.0.0.1:4222 57 INFO 2023-04-18T20:30:58.755Z context=pubsub Starting NATS pub/sub: nats://127.0.0.1:4222 58 ``` 59 60 See [configuration](./configuration.md) for available NATS configuration settings.