NATS Server Clients
A NATS client is an application making a connection to one of the nats servers pointed to by its connection URL, and uses a credential file to authenticate and indicate its authorization to the server and the whole NATS infrastructure.
The nats-server doesn't come bundled with any clients, but its companion is the nats
CLI tool that you should install (even if you don't intend to run your own servers) as it is the best tool to use to test, monitor, manage and generally interact with a NATS infrastructure (regardless of that infrastructure being an isolated local server, a leaf node server, a cluster or even a global super-cluster).
Other NATS client tools to know about are the nsc
CLI tool (to manage accounts attributes and user JWT tokens) and the 'nk' tool (and library) to manage Nkeys.
Also, most client libraries come with sample programs that allow you to publish, subscribe, send requests and reply messages.
Embedding NATS
If your application is in Go, and if it fits your use case and deployment scenarios, you can even embed a NATS server inside your application.
Installing the nats
CLI Tool
nats
CLI ToolFrom the shell (all platforms), to get the binary for the latest (top of the main
branch) version of nats
you can simply do:
You can also get a specific version by adding @
followed by the version's tag, e.g. curl -sf https://binaries.nats.dev/nats-io/natscli/nats@v0.1.4 | sh
For macOS:
For Arch Linux:
Binaries are also available as GitHub Releases.
Testing your setup
Open a terminal and start a nats-server:
On another terminal session first check the connection to the server
Next, start a subscriber using the nats
CLI tool:
Note that when the client connected, the server didn't log anything interesting because server output is relatively quiet unless something interesting happens.
To make the server output more lively, you can specify the -V
flag to enable logging of server protocol tracing messages. Go ahead and <ctrl>+c
the process running the server, and restart the server with the -V
flag:
If you had created a subscriber, you should notice output on the subscriber telling you that it disconnected, and reconnected. The server output above is more interesting. You can see the subscriber send a CONNECT
protocol message and a PING
which was responded to by the server with a PONG
.
You can learn more about the NATS protocol here, but more interesting than the protocol description is an interactive demo.
On a third terminal, publish your first message:
On the subscriber window you should see:
Testing Against a Remote Server
If the NATS server were running in a different machine or a different port, you'd have to specify that to the client by specifying a NATS URL (either in a nats context
or using the -s
flag).
NATS URLs
NATS URLs take the form of: nats://<server>:<port>
and tls://<server>:<port>
. URLs with a tls
protocol sport a secured TLS connection.
If you are connecting to a cluster you can specify more than one URL (comma separated). e.g. nats://localhost:4222,nats://localhost:5222,nats://localhost:6222
if you are running a test cluster of 3 nats servers on your local machine, listening at ports 4222, 5222, and 6222 respectively.
Example
If you want to try on a remote server, the NATS team maintains a demo server you can reach at demo.nats.io
.
Last updated