nats

A command line utility to interact with and manage NATS.

This utility replaces various past tools that were named in the form nats-sub and nats-pub, adds several new capabilities and supports full JetStream management.

Check out the repo for all the details: github.com/nats-io/natscli.

Installing nats

For macOS:

  1. brew tap nats-io/nats-tools
  2. brew install nats-io/nats-tools/nats

For Arch Linux:

Download the correct .deb file for your computer from here.

If you have an Intel CPU, then it’ll probably be this one (for version X.Y.Z): nats-X.Y.Z-amd64.deb Then run this command to install the file.

  1. sudo dpkg -i nats-X.Y.Z-amd64.deb

Or with the yay package manager

  1. yay natscli

Binaries are also available as GitHub Releases.

Using nats

Getting help

  • NATS Command Line Interface README
  • nats help
  • nats help [<command>...] or nats [<command>...] --help
  • Remember to look at the cheat sheets!
    • nats cheat
    • nats cheat --sections
    • nats cheat <section>>

Interacting with NATS

  • nats context
  • nats account
  • nats pub
  • nats sub
  • nats request
  • nats reply
  • nats bench

Monitoring NATS

  • nats events
  • nats rtt
  • nats server
  • nats latency
  • nats governor

Managing and interacting with streams

  • nats stream
  • nats consumer
  • nats backup
  • nats restore

Managing and interacting with the K/V Store

  • nats kv

Get reference information

  • nats errors
  • nats schema

Configuration Contexts

The CLI has a number of configuration settings that can be passed either as command line arguments or set in environment variables.

  1. nats --help

Output extract

  1. ...
  2. -s, --server=URL NATS server urls ($NATS_URL)
  3. --user=USER Username or Token ($NATS_USER)
  4. --password=PASSWORD Password ($NATS_PASSWORD)
  5. --creds=FILE User credentials ($NATS_CREDS)
  6. --nkey=FILE User NKEY ($NATS_NKEY)
  7. --tlscert=FILE TLS public certificate ($NATS_CERT)
  8. --tlskey=FILE TLS private key ($NATS_KEY)
  9. --tlsca=FILE TLS certificate authority chain ($NATS_CA)
  10. --timeout=DURATION Time to wait on responses from NATS
  11. ($NATS_TIMEOUT)
  12. --context=NAME Configuration context ($NATS_CONTEXT)
  13. ...

The server URL can be set using the --server CLI flag, or the NATS_URL environment variable, or using NATS Contexts.

The password can be set using the --password CLI flag, or the NATS_PASSWORD environment variable, or using NATS Contexts. For example: if you want to create a script that prompts the user for the system user password (so that for example it doesn’t appear in ps or history or maybe you don’t want it stored in the profile) and then execute one or more nats commands you do something like:

  1. #!/bin/bash
  2. echo "-n" "system user password: "
  3. read -s NATS_PASSWORD
  4. export NATS_PASSWORD
  5. nats server report jetstream --user system

NATS Contexts

A context is a named configuration that stores all of these settings. You can designate a default context and switch between contexts.

A context can be created with nats context create my_context_name and then modified withnats context edit my_context_name:

  1. {
  2. "description": "",
  3. "url": "nats://127.0.0.1:4222",
  4. "token": "",
  5. "user": "",
  6. "password": "",
  7. "creds": "",
  8. "nkey": "",
  9. "cert": "",
  10. "key": "",
  11. "ca": "",
  12. "nsc": "",
  13. "jetstream_domain": "",
  14. "jetstream_api_prefix": "",
  15. "jetstream_event_prefix": "",
  16. "inbox_prefix": "",
  17. "user_jwt": ""
  18. }

This context is stored in the file ~/.config/nats/context/my_context_name.json.

A context can also be created by specifying settings with nats context save

  1. nats context save example --server nats://nats.example.net:4222 --description 'Example.Net Server'
  2. nats context save local --server nats://localhost:4222 --description 'Local Host' --select

List your contexts

  1. nats context ls
  1. Known contexts:
  2. example Example.Net Server
  3. local* Local Host

We passed --select to the local one meaning it will be the default when nothing is set.

Select a context

  1. nats context select

Check the round trip time to the server (using the currently selected context)

  1. nats rtt
  1. nats://localhost:4222:
  2. nats://127.0.0.1:4222: 245.115µs
  3. nats://[::1]:4222: 390.239µs

You can also specify a context directly

  1. nats rtt --context example
  1. nats://nats.example.net:4222:
  2. nats://192.0.2.10:4222: 41.560815ms
  3. nats://192.0.2.11:4222: 41.486609ms
  4. nats://192.0.2.12:4222: 41.178009ms

All nats commands are context aware and the nats context command has various commands to view, edit and remove contexts.

Server URLs and Credential paths can be resolved via the nsc command by specifying an URL, for example to find user new within the orders account of the acme operator you can use this:

  1. nats context save example --description 'Example.Net Server' --nsc nsc://acme/orders/new

The server list and credentials path will now be resolved via nsc, if these are specifically set in the context, the specific context configuration will take precedence.

Generating bcrypted passwords

The server supports hashing of passwords and authentication tokens using bcrypt. To take advantage of this, simply replace the plaintext password in the configuration with its bcrypt hash, and the server will automatically utilize bcrypt as needed. See also: Bcrypted Passwords.

The nats utility has a command for creating bcrypt hashes. This can be used for a password or a token in the configuration.

  1. nats server passwd
  1. ? Enter password [? for help] **********************
  2. ? Reenter password [? for help] **********************
  3. $2a$11$3kIDaCxw.Glsl1.u5nKa6eUnNDLV5HV9tIuUp7EHhMt6Nm9myW1aS

To use the password on the server, add the hash into the server configuration file’s authorization section.

  1. authorization {
  2. user: derek
  3. password: $2a$11$3kIDaCxw.Glsl1.u5nKa6eUnNDLV5HV9tIuUp7EHhMt6Nm9myW1aS
  4. }

Note the client will still have to provide the plain text version of the password, the server however will only store the hash to verify that the password is correct when supplied.

See Also

Publish-subscribe pattern using the NATS CLI

{% embed url=”https://www.youtube.com/watch?v=jLTVhP08Tq0“ %} Publish-subscribe Pattern using NATS CLI {% endembed %}