github.com/anycable/anycable-go@v1.5.1/docs/embedded_nats.md (about) 1 # Embedded NATS 2 3 AnyCable supports running a NATS server as a part of the `anycable-go` WebSocket server. Thus, you don't need any _external_ pub/sub services to build AnyCable clusters (i.e., having multiple WebSocket nodes). 4 5 > 🎥 Check out this [AnyCasts episode](https://anycable.io/anycasts/flying-multi-regionally-with-nats/) to learn how to use AnyCable with embedded NATS on [Fly.io][fly] 6 7 There are multiple ways to use this functionality: 8 9 - [Single-server configuration](#single-server-configuration) 10 - [Cluster configuration](#cluster-configuration) 11 12 ## Single-server configuration 13 14 The easiest way to start using embedded NATS in AnyCable is to run a single `anycable-go` instance with _eNATS_ (this is how we call "Embedded NATS") enabled and connecting all other instances to it. This is how you can do that locally: 15 16 ```sh 17 # first instance with NATS embedded 18 $ anycable-go --broadcast_adapter=nats --embed_nats --enats_addr=nats://0.0.0.0:4242 19 20 INFO 2023-02-28T00:06:45.618Z context=main Starting AnyCable 1.3.0 21 INFO 2023-02-28T00:06:45.649Z context=main Embedded NATS server started: nats://127.0.0.1:4242 22 ``` 23 24 Now you can run another WebSocket server connected to the first one: 25 26 ```sh 27 anycable-go --port 8081 --broadcast_adapter=nats --nats_servers=nats://0.0.0.0:4242 28 ``` 29 30 RPC servers can also connect to the first AnyCable-Go server: 31 32 ```sh 33 bundle exec anycable --broadcast_adapter=nats --nats_servers=nats://0.0.0.0:4242 34 ``` 35 36 This setup is similar to running a single NATS server independently. 37 38 ## Cluster configuration 39 40 Alternatively, you can form a cluster from embedded NATS instances. For that, you should start each `anycable-go` instance with a NATS cluster address and connect them together via the routes table: 41 42 ```sh 43 # first instance 44 $ anycable-go --broadcast_adapter=nats --embed_nats --enats_addr=nats://0.0.0.0:4242 --enats_cluster=nats://0.0.0.0:4243 45 46 INFO 2023-02-28T00:06:45.618Z context=main Starting AnyCable 1.3.0 47 INFO 2023-02-28T00:06:45.649Z context=main Embedded NATS server started: nats://127.0.0.1:4242 (cluster: nats://0.0.0.0:4243, cluster_name: anycable-cluster) 48 49 # other instances 50 $ anycable-go --port 8081 --broadcast_adapter=nats --embed_nats --enats_addr=nats://0.0.0.0:4342 --enats_cluster=nats://0.0.0.0:4343 --enats_cluster_routes=nats://0.0.0.0:4243 51 52 INFO 2023-02-28T00:06:45.618Z context=main Starting AnyCable 1.3.0 53 INFO 2023-02-28T00:06:45.649Z context=main Embedded NATS server started: nats://127.0.0.1:4342 (cluster: nats://0.0.0.0:4343, cluster_name: anycable-cluster, routes: nats://0.0.0.0:4243) 54 ``` 55 56 See more information in the [NATS documentation](https://docs.nats.io/running-a-nats-service/configuration/clustering). 57 58 ### Using on Fly.io 59 60 AnyCable automatically infers sensible default configuration values for applications deployed to Fly.io. 61 62 To configure a cluster from embedded NATS servers, all you need is to turn the embedded NATS feature on and use the defaults. AnyCable automatically configures cluster addresses and routes to build a cluster **within the current region**. 63 64 See also [Fly deployment documentation](../deployment/fly.md). 65 66 ### Super-cluster 67 68 You can also setup a super-cluster by configuring gateways: 69 70 ```sh 71 # first cluster 72 $ anycable-go --broadcast_adapter=nats --embed_nats \ 73 --enats_addr=nats://0.0.0.0:4242 --enats_cluster=nats://0.0.0.0:4243 \ 74 --enats_gateway=nats://0.0.0.0:7222 75 76 # second cluster 77 $ anycable-go --port 8081 --broadcast_adapter=nats --embed_nats \ 78 --enats_addr=nats://0.0.0.0:4342 --enats_cluster=nats://0.0.0.0:4343 \ 79 --enats_gateway=nats://0.0.0.0:7322 \ 80 --enats_gateways=anycable-cluster:nats://0.0.0.0:7222 81 ``` 82 83 **NOTE**: The value of the `--enats_gateways` parameter must be have a form `<name>:<addr-1>,<addr-2>;<name-2>:<addr-3>,<addr-4>`. 84 85 **IMPORTANT**: All servers in the cluster must have the same gateway configuration. 86 87 You can also specify the advertised address for the gateway (in case your cluster is behind a NAT) via the `--enats_gateway_advertise` parameter. 88 89 See more information in the [NATS documentation](https://docs.nats.io/running-a-nats-service/configuration/clustering). 90 91 [fly]: https://fly.io