nats CLI

    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.

    For macOS:

    For Arch Linux:

    Download the correct .deb file for your computer from .

    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.

    • 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 request
    • nats reply
    • nats bench
    • nats events
    • nats rtt
    • nats server
    • nats latency
    • nats governor

      Managing and interacting with streams

    • nats consumer
    • nats backup
    • nats restore
    • nats kv

      Get reference information

    • nats errors
    • nats schema

    In practice, it is quite common for the administrators of a NATS service infrastructure to have to connect using various NATS URLs and security credentials, the CLI has a number of environment configuration settings that can be passed as command line arguments or set in environment variables. In order to facilitate switching between NATS environments or servers, clusters, operators, etc… nats lets you use ‘contexts’ that you can store and easily select.

    1. nats --help

    Output extract

    1. ...
    2. -s, --server=NATS_URL NATS servers
    3. --user=NATS_USER Username of Token
    4. --password=NATS_PASSWORD Password
    5. --creds=NATS_CREDS User credentials
    6. --nkey=NATS_NKEY User NKEY
    7. --tlscert=NATS_CERT TLS public certificate
    8. --tlsca=NATS_CA TLS certificate authority chain
    9. --timeout=NATS_TIMEOUT Time to wait on responses from NATS
    10. --context=CONTEXT NATS Configuration Context to use for access
    11. ...

    A context is a named configuration that stores all of these settings, you can switch between access configurations and designate a default.

    Creating one is easy, just specify the settings with nats context save

    Or you can use nats context create my_context_name and then edit the created context file (i.e. in ~/.config/nats/context/my_context_name.json)

    List your contexts

    1. nats context ls

    Output

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

    We passed 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

    Output

    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.

    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.

    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

    Output

    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.

    Publish-subscribe pattern using the NATS CLI

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