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 -L https://github.com/nats-io/nats-server/releases/latest/download/nats-server-linux-amd64.zip -o nats-server.zip
unzip nats-server.zip
sudo cp 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 -L https://github.com/nats-io/natscli/releases/latest/download/nats-linux-amd64.zip -o nats-cli.zip
unzip nats-cli.zip
sudo cp 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:

  1. Request-Reply Pattern - Synchronous communication
  2. Queue Groups - Load balancing
  3. Subjects - Understanding subject-based messaging
  4. Pub/Sub Basics - Core messaging patterns

Client Libraries

NATS has official clients for:

Resources