{"id":20697956,"url":"https://github.com/fly-apps/nats-cluster","last_synced_at":"2025-04-22T21:28:20.535Z","repository":{"id":38681130,"uuid":"313457605","full_name":"fly-apps/nats-cluster","owner":"fly-apps","description":"Global messaging for apps that need to talk to each other.","archived":false,"fork":false,"pushed_at":"2023-08-10T04:24:13.000Z","size":33,"stargazers_count":60,"open_issues_count":4,"forks_count":24,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T19:01:52.262Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fly-apps.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-16T23:55:17.000Z","updated_at":"2025-03-13T22:28:53.000Z","dependencies_parsed_at":"2023-02-13T01:15:16.575Z","dependency_job_id":null,"html_url":"https://github.com/fly-apps/nats-cluster","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fly-apps%2Fnats-cluster","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fly-apps%2Fnats-cluster/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fly-apps%2Fnats-cluster/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fly-apps%2Fnats-cluster/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fly-apps","download_url":"https://codeload.github.com/fly-apps/nats-cluster/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250327394,"owners_count":21412421,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-17T00:20:55.988Z","updated_at":"2025-04-22T21:28:20.515Z","avatar_url":"https://github.com/fly-apps.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Global NATS Cluster\n\n[NATS](https://docs.nats.io/) is an open source messaging backend suited to many use cases and deployment scenarios. We use it for internal communications at Fly. This repo shows how to use it for your application.\n\nThis example creates a federated mesh of NATS servers that communicate over the private, encrypted IpV6 network available to all Fly organizations.\n## Setup\n\n1. `fly launch --no-deploy`\n\n    \u003e You'll be prompted for an app name. Hit return to let Fly generate an app name for you. Pick your target organizatio and a starting region.\n\n2. `flyctl deploy`\n\n    \u003e This will start NATS with a single node in your selected region.\n\n3. Add more regions with `flyctl regions add \u003cregion\u003e` or\n\n    \u003e For this demo, we set `ord`, `syd`, `cdg` regions.\n\n```cmd\nfly regions set ord syd cdg\n```\n\n4. Scale the application so it can place nodes in the regions.\n\n```cmd\nfly scale count 3\n```\n\nThen run `flyctl logs` and you'll see the virtual machines discover each other.\n\n```\n2020-11-17T17:31:07.664Z d1152f01 ord [info] [493] 2020/11/17 17:31:07.646272 [INF] [fdaa:0:1:a7b:abc:21de:af5f:2]:4248 - rid:1 - Route connection created\n2020-11-17T17:31:07.713Z 21deaf5f cdg [info] [553] 2020/11/17 17:31:07.704807 [INF] [fdaa:0:1:a7b:81:d115:2f01:2]:34902 - rid:19 - Route connection created\n2020-11-17T17:31:08.123Z 82fabc30 syd [info] [553] 2020/11/17 17:31:08.114852 [INF] [fdaa:0:1:a7b:81:d115:2f01:2]:4248 - rid:7 - Route connection created\n2020-11-17T17:31:08.259Z d1152f01 ord [info] [493] 2020/11/17 17:31:08.241644 [INF] [fdaa:0:1:a7b:b92:82fa:bc30:2]:45684 - rid:2 - Route connection created\n```\n\n## Testing the cluster\n\nWhile the cluster is only accessible from inside the Fly network, you can use Fly's [Wireguard support](/docs/reference/privatenetwork/) to create a VPN into your Fly organisation and private network.\n\nThen you can use tools such as [natscli](https://github.com/nats-io/natscli) to subscribe to topics, publish messages to topics and perform various tests on your NATS cluster. Install the tool first.\n\nOnce installed, create a context that points at your NATS cluster:\n\n```cmd\nnats context add fly.demo --server appname.internal:4222 --description \"My Cluster\" --select\n```\n\nYou can subscribe to a topic with `nats sub topicname`:\n\n```cmd\nnats sub fly.demo\n```\n\nAnd then, in another terminal sessions, we can use `nats pub topicname` to send either simple messages to that topic:\n\n```cmd\nnats pub fly.demo \"Hello World\"\n```\n\nOr send multiple messages:\n\n```cmd\nnats pub fly.demo \"fly.demo says {{.Cnt}} @ {{.TimeStamp}}\" --count=10\n```\n\nYou're ready to start integrating NATS messaging into your other Fly applications.\n\n## What to try next\n\n1. [NATS streaming](https://docs.nats.io/nats-streaming-concepts/intro) offers persistence features, you can create a NATS streaming app by modifying this demo and adding volumes: `flyctl volume create`\n\n2. Create a [NATS super cluster](https://docs.nats.io/nats-server/configuration/gateways) let you join multiple NATS clusters with gateways. If you want to run regional clusters, you can query the Fly DNS service to with `\u003cregion\u003e.\u003capp-name\u003e.internal` to find server in specific regions.\n\n\n## Discuss\n\nYou can discuss this example (and the paired 6pn-demo-chat example) on the [dedicated Fly Community topic](https://community.fly.io/t/new-examples-nats-cluster-and-6pn-demo-chat/562).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffly-apps%2Fnats-cluster","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffly-apps%2Fnats-cluster","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffly-apps%2Fnats-cluster/lists"}