Skip to main content

Getting Started with NATS

Get up and running with NATS in minutes. This guide will walk you through installation, basic setup, and your first NATS application.

Installation​

Quick Start with Docker​

The fastest way to get NATS running:

docker run -p 4222:4222 -p 8222:8222 nats:latest

This starts NATS Server with:

  • Client connections on port 4222
  • HTTP monitoring on port 8222

Install NATS Server​

macOS​

brew install nats-server

Linux​

curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@latest | sh
sudo mv nats-server /usr/local/bin/

Windows​

Download the latest release from GitHub Releases.

Verify Installation​

nats-server --version

Start NATS Server​

Basic Server​

nats-server

With Monitoring​

nats-server -m 8222

Visit http://localhost:8222 to see server metrics.

With JetStream (Persistence)​

nats-server -js

Install NATS CLI​

The NATS CLI tool helps you interact with NATS:

# macOS
brew install nats-io/nats-tools/nats

# Linux
curl -sf https://binaries.nats.dev/nats-io/natscli/nats@latest | sh
sudo mv nats /usr/local/bin/

Your First NATS Application​

Using the CLI​

# Subscribe to a subject
nats sub hello &

# Publish a message
nats pub hello "Hello NATS!"

Install Client Libraries​

# The NATS CLI is already installed (see above)
# You can use it directly for pub/sub operations

Publisher Example​

#!/bin/bash

# Publish a message to demo.nats.io
nats pub --server=demo.nats.io hello "Hello NATS!"

Subscriber Example​

#!/bin/bash

# Subscribe to messages from demo.nats.io
nats sub --server=demo.nats.io hello

Running the Examples​

# Terminal 1 - Start subscriber
nats sub hello

# Terminal 2 - Publish messages
nats pub hello "Hello NATS!"
nats pub hello "Welcome to messaging"

# Request-Reply pattern
# Terminal 1 - Start replier
nats reply hello "Hi there!"

# Terminal 2 - Send request
nats request hello "Anyone there?" --timeout=2s

Next Steps​

Congratulations! You've successfully:

  • ✅ Installed NATS Server
  • ✅ Published and subscribed to messages
  • ✅ Built your first NATS application

What to explore next:​

Client Libraries​

NATS has official clients for:

Resources​