# NATS notes/tips

**URL:** <https://community.tmpdir.org/t/nats-notes-tips/340>\
**Category:** Tools\
**Tags:** nats\
**Created:** [October 22, 2021, 1:35pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340 "2021-10-22T13:35:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [October 22, 2021, 1:35pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/1 "2021-10-22T13:35:36Z")

</div>

As I am digging more into advanced features of NATS (auth, kv store, Jetstream, NGS, etc), I thought it might be useful to put together a series of notes on how to do various things. Through this series, I will be using the Synadia NGS hosted offering as well as a local server.

_Feel free to add your own entries of NATS tips to this thread._

# Install the tools

- [`nats`](https://docs.nats.io/nats-tools/natscli): NATS Utility, server and JetStream admin
- [`nsc`](https://docs.nats.io/nats-tools/nsc): manage operators, accounts, users and permissions
- [`ngs`](https://synadia.com/ngs/signup): used to manage your Synadia NATS Global service account
- [`kv`](https://docs.nats.io/nats-tools/nk): generate NKeys
- [`nats-server`](https://docs.nats.io/nats-server/installation): the server itself

Arch Linux has `nats-server` and `natscli-bin` packages in the AUR, which I installed with `yay`.

# nats context

The `nats` utility has a useful feature called contexts. This allows you to set up various contexts and quickly switch between them. Lets set a context for the local server:

```bash
nats context save local
nats context select local

```

This creates and selects a default context which connects to your a local server.

```json
more /home/cbrake/.config/nats/context/local.json
{
  "description": "",
  "url": "nats://127.0.0.1:4222",
  "token": "",
  "user": "",
  "password": "",
  "creds": "",
  "nkey": "",
  "cert": "",
  "key": "",
  "ca": "",
  "nsc": "",
  "jetstream_domain": "",
  "jetstream_api_prefix": "",
  "jetstream_event_prefix": ""
}

```

# Basic operations with local server

```bash
nats-server 
[91720] 2021/10/22 09:17:19.187352 [INF] Starting nats-server
[91720] 2021/10/22 09:17:19.187403 [INF] Version: 2.6.1
[91720] 2021/10/22 09:17:19.187407 [INF] Git: [c91f0fe]
[91720] 2021/10/22 09:17:19.187414 [INF] Name: NCH4L4TAGXQK3QXTUTZXEPZJHOOGZRJVGPPMDQYFFKQ2EJHNUO2BMSMS
[91720] 2021/10/22 09:17:19.187418 [INF] ID: NCH4L4TAGXQK3QXTUTZXEPZJHOOGZRJVGPPMDQYFFKQ2EJHNUO2BMSMS
[91720] 2021/10/22 09:17:19.187982 [INF] Listening for client connections on 0.0.0.0:4222
[91720] 2021/10/22 09:17:19.188214 [INF] Server is ready

```

```auto
[cbrake@ceres ~]$ nats sub test
09:17:35 Subscribing on test
[#1] Received on "test"
hi

[#2] Received on "test"
hi

```

```auto
[cbrake@ceres ~]$ nats pub test "hi"
09:26:16 Published 2 bytes to "test"
[cbrake@ceres ~]$ nats pub test "hi"
09:26:17 Published 2 bytes to "test"

```

# Pub/Sub with NGS account

- create an account as described [here](https://synadia.com/ngs/signup)
  - create account: `nsc init -o synadia -n Cliff`
  - check status limits: `ngs status`

- Set up context for NGS
  - create context: `nats context save ngs`
  - edit context: `nats context edit ngs`
    - change the following lines
    - `"url": "connect.ngs.global"`
    - `"creds": "/home/cbrake/.nkeys/creds/synadia/Cliff/Cliff.creds"`

  - select context: `nats context select ngs`

(_It seems the complete path is required for `creds` to work_)

Now you should be able to `nats sub` and `nats pub` without your local server running as it will use the NGS global NATS servers.

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [October 22, 2021, 2:22pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/2 "2021-10-22T14:22:02Z")

</div>

# Leaf nodes

Create a file named `nsc-leaf.conf`:

```auto
host: 127.0.0.1

leafnodes {
  remotes = [
    {
        url: "tls://connect.ngs.global"
        creds: "~/.nkeys/creds/synadia/Cliff/Cliff.creds"
    }
  ]
}

```

Start the NATS server with the above config:

- `nats-server -c ngs-leaf.conf`

However, I got errors because the free plan does not support leaf nodes, so we need to change our plan to the developer:

- `ngs edit`
- select developer plan
- you will then receive an email to verify email address and enter credit card #. Your card will not be charged with the developer plan.

 ![image](https://community.tmpdir.org/uploads/default/original/1X/12e6d5d484b41ff2b0eeed288026a003b07e1069.png)

Run `nsc pull -A` to sync your account information.

Now, I can `sub` a topic on my NGS server account, `pub` to a topic on my local server, and because the local server is a leaf node of NGS, the message will be sent to NGS.

```auto
> nats context select ngs
> nats sub test
10:35:26 Subscribing on test
[#1] Received on "test"
hi there

[#2] Received on "test"
hi there

```

```auto
> nats context select local
NATS Configuration Context "local"

      Server URLs: nats://127.0.0.1:4222
             Path: /home/cbrake/.config/nats/context/local.json

[cbrake@ceres ~]$ nats pub test "hi there"
10:35:50 Published 8 bytes to "test"
[cbrake@ceres ~]$ nats pub test "hi there"
10:35:52 Published 8 bytes to "test"

```

Next steps:

- leaf node subscribes to a subset of topic namespace
- accounts
- authz so leaf node only has access to his subject namespace
- JetStream
- kv store

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [February 25, 2022, 4:35pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/3 "2022-02-25T16:35:56Z")

</div>

> **[Embedding NATS in Go](https://dev.to/karanpratapsingh/embedding-nats-in-go-19o)**
>
> In this article, we will look at how we can embed NATS server within our Go application. After that,...

We do this in Simple IoT – it works great!

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [September 12, 2022, 4:29pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/4 "2022-09-12T16:29:00Z")

</div>

NATS 2.9.0 released:

> **[Release Release v2.9.0 · nats-io/nats-server](https://github.com/nats-io/nats-server/releases/tag/v2.9.0)**
>
> Changelog
> IMPORTANT NOTE: Leafnode connections will now be closed if the cluster name is detected to be the same on the "hub" and "spoke", and reconnect attempts will be delayed by 30 seconds. If y...

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [October 11, 2023, 9:33pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/5 "2023-10-11T21:33:26Z")

</div>

NATS 2.10 has been released:

> **[NATS Server 2.10 Release](https://nats.io/blog/nats-server-2.10-release/)**
>
> It’s finally here! 🤩 The NATS team is thrilled to announce the 2.10 release of the NATS Server!
> We want to thank everyone who contributed through discussions, raising issues, code contribution and testing early versions. ❤️
> The journey to 2.10 Before...

> **[NATS 2.10](https://docs.nats.io/release-notes/whats_new/whats_new_210)**

https://www.youtube.com/embed/9J4pRzHSc2k?feature=oembed&wmode=opaque&list=PLgqCaaYodvKb6BJ-p7sJEWXy20IkZe4je

A few things caught my attention:

- the extreme amount of testing that is going into NATS
- they are designing it to scale
- a lot of focus went into making the jetstream disk format more efficient.
- there is now an [Auth Callout](https://docs.nats.io/running-a-nats-service/configuration/securing_nats/auth_callout) extension point.

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [October 11, 2023, 9:49pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/6 "2023-10-11T21:49:46Z")

</div>

One other thing that is interesting is the NATS KV and Object store are client-side abstractions on top of Jetstream. No extra server functionality is required. This is pretty neat.

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [January 3, 2024, 7:24pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/7 "2024-01-03T19:24:40Z")

</div>

# Evaluating Jetstream as a store for Simple IoT

The [nats-exp](https://github.com/simpleiot/nats-exp)[GitHub - simpleiot/nats-exp: NATS Experiements · GitHub](https://github.com/simpleiot/nats-exp) repo has several experiments where we attempt to determine if we can use NATS Jetstream as a store for Simple IoT. So far, it looks promising.

## Time-series metrics:

```auto
2024/01/03 14:20:10 NATS TSD experiment
2024/01/03 14:20:10 js.Publish insert rate for 10,000 points: 42028 pts/sec
2024/01/03 14:20:10 js.PublishAsync insert rate for 10,000 points: 412363 pts/sec
2024/01/03 14:20:10 nats.Publish insert rate for 10,000 points: 17937960 pts/sec
2024/01/03 14:20:10 ack: false, Get 30000 points took 0.04, 668062 pts/sec
2024/01/03 14:20:10 ack: true, Get 30000 points took 0.04, 827364 pts/sec
2024/01/03 14:20:10 ack: false, Get 30000 points took 0.03, 881960 pts/sec

```

Seems fast enough.

## Syncing streams between hub and leaf nodes:

```auto
[cbrake@quark nats-exp]$ go run ./bi-directional-sync/
=====================================================
       Create stream on hub and source to leaf
=====================================================
* Create stream: server:hub domain:hub stream:NODES-HUB subject:n.hub.>
* Publish: hub -> n.hub.123.value -> 12 13 14 15
* Stream count: server:hub domain:hub stream:NODES-HUB count:4
* Stream count: server:leaf domain:hub stream:NODES-HUB count:4
* Source stream: server:leaf domain:leaf stream:NODES-HUB source-domain:hub subject:n.hub.>
* Stream count: server:leaf domain:leaf stream:NODES-HUB count:4
=====================================================
       Create stream on leaf and source to hub
=====================================================
* Create stream: server:leaf domain:leaf stream:NODES-LEAF subject:n.leaf.>
* Publish: leaf -> n.leaf.456.value -> 22 23 24 25 26
* Source stream: server:hub domain:hub stream:NODES-LEAF source-domain:leaf subject:n.leaf.>
* Stream count: server:hub domain:hub stream:NODES-LEAF count:5
=====================================================
       Shut down leaf node
=====================================================
* Stream count: server:hub domain:hub stream:NODES-LEAF count:5
* Publish: hub -> n.hub.123.value -> 16 17 18 19
* Stream count: server:hub domain:hub stream:NODES-HUB count:8
=====================================================
       Start up leaf node
=====================================================
* Stream count: server:leaf domain:leaf stream:NODES-HUB count:8
* Publish: hub -> n.hub.123.value -> 20 21 22 23
* Stream count: server:leaf domain:leaf stream:NODES-HUB count:12
=====================================================
       Publish more messages to leaf
=====================================================
* Publish: leaf -> n.leaf.456.value -> 27 28 29 30 31
* Stream count: server:leaf domain:leaf stream:NODES-LEAF count:10
* Stream count: server:leaf domain:leaf stream:NODES-LEAF count:10

```

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [February 20, 2025, 3:10pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/8 "2025-02-20T15:10:46Z")

</div>

# NATS Cheatsheet

A good overview:

 ![nats-cheatsheet](https://community.tmpdir.org/uploads/default/original/2X/9/9f4d3926a25d4ba50b5cd7b8daec252a4a4420af.jpeg)

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [March 24, 2025, 8:29pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/9 "2025-03-24T20:29:47Z")

</div>

> **[NATS Server 2.11 Release](https://nats.io/blog/nats-server-2.11-release/)**
>
> We are thrilled to announce that NATS Server 2.11 is finally here!
> We want to extend a heartfelt thank you to everyone who contributed through discussions, issue reporting, code contributions, and testing early versions. ❤️
> A Long Journey to 2.11 The...

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [November 11, 2026, 5:02pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/10 "2026-11-11T17:02:36Z")

</div>

> **[CNCF Announces NATS Graduation, Solidifying a Production-Ready Foundation for...](https://www.cncf.io/announcements/2026/11/10/cncf-announces-nats-graduation-solidifying-a-production-ready-foundation-for-messaging-across-cloud-and-edge/)**
>
> Open source project used by more than 2,000 companies spanning AI, retail, financial services, and telecommunications Key Highlights KUBECON + CLOUDNATIVECON NORTH AMERICA, SALT LAKE CITY – November…

---

<div class="post-metadata">

**Author:** ![cbrake](https://community.tmpdir.org/user_avatar/community.tmpdir.org/cbrake/32/3_2.png) [@cbrake](https://community.tmpdir.org/u/cbrake)\
**Post date:** [November 11, 2026, 5:02pm UTC](https://community.tmpdir.org/t/nats-notes-tips/340/11 "2026-11-11T17:02:56Z")

</div>

> **[How Lovable Moved Its Frontend off Firestore and Built Lovable Realtime on NATS](https://www.synadia.com/blog/lovable-realtime-nats)**
>
> Lovable replaced browser-side Firestore listeners with its own realtime service on NATS.
