Warning: This document is old! It is likely wrong in some important way.
Global NATS Cluster
NATS 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.
This example creates a federated mesh of NATS servers that communicate over the private, encrypted IpV6 network available to all Fly organizations.
Setup
-
fly launch --no-deploy
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.
-
flyctl deploy
This will start NATS with a single node in your selected region.
-
Add more regions with
flyctl regions add <region> or
For this demo, we set ord, syd, cdg regions.
- Scale the application so it can place nodes in the regions.
Then run flyctl logs and you’ll see the virtual machines discover each other.
Testing the cluster
While the cluster is only accessible from inside the Fly network, you can use Fly’s Wireguard support to create a VPN into your Fly organisation and private network.
Then you can use tools such as natscli to subscribe to topics, publish messages to topics and perform various tests on your NATS cluster. Install the tool first.
Once installed, create a context that points at your NATS cluster:
You can subscribe to a topic with nats sub topicname:
And then, in another terminal sessions, we can use nats pub topicname to send either simple messages to that topic:
Or send multiple messages:
You’re ready to start integrating NATS messaging into your other Fly applications.
What to try next
-
NATS streaming offers persistence features, you can create a NATS streaming app by modifying this demo and adding volumes:
flyctl volume create
-
Create a NATS super cluster let you join multiple NATS clusters with gateways. If you want to run regional clusters, you can query the Fly DNS service to with
<region>.<app-name>.internal to find server in specific regions.
Discuss
You can discuss this example (and the paired 6pn-demo-chat example) on the dedicated Fly Community topic.