How to Install NATS for Lightweight Messaging

NATS is a lightweight, extremely fast messaging system — well suited for simple pub/sub patterns and microservices communication where RabbitMQ's additional features would be unnecessary overhead.

NATS vs RabbitMQ

FactorNATSRabbitMQ
Resource footprintVery lightweight (single small binary)Heavier (Erlang runtime)
ThroughputVery highHigh, but generally lower than NATS
Message persistenceOptional (via JetStream)Built-in, more mature
ComplexitySimpler to run and reason aboutMore features, more complexity

Prerequisites

  • Ubuntu 22.04/24.04 or Debian 11/12 VPS
  • Root or sudo access

Step 1 — Install NATS Server

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

Step 2 — Create a Configuration File

sudo nano /etc/nats/nats-server.conf
port: 4222
http_port: 8222

authorization {
  user: myapp
  password: CHANGE_ME_STRONG_PASSWORD
}

jetstream {
  store_dir: "/var/lib/nats/jetstream"
}

Step 3 — Create a systemd Service

sudo nano /etc/systemd/system/nats.service
[Unit]
Description=NATS Server
After=network.target

[Service]
ExecStart=/usr/local/bin/nats-server -c /etc/nats/nats-server.conf
Restart=always
User=nats

[Install]
WantedBy=multi-user.target
sudo useradd -r -s /bin/false nats
sudo mkdir -p /var/lib/nats/jetstream
sudo chown -R nats:nats /var/lib/nats
sudo systemctl daemon-reload
sudo systemctl enable --now nats

Step 4 — Allow the Required Port

sudo ufw allow 4222/tcp

Step 5 — Install the NATS CLI (Optional, for Testing)

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

Basic Pub/Sub Example (Node.js)

const { connect } = require('nats');

const nc = await connect({
  servers: 'nats://YOUR_SERVER_IP:4222',
  user: 'myapp',
  pass: 'CHANGE_ME_STRONG_PASSWORD',
});

// Subscriber
const sub = nc.subscribe('orders.created');
for await (const msg of sub) {
  console.log('Received:', msg.data.toString());
}
// Publisher
nc.publish('orders.created', Buffer.from(JSON.stringify({ orderId: 123 })));

Using JetStream for Persistent Messaging

Basic NATS pub/sub is fire-and-forget (subscribers must be connected to receive a message). For guaranteed delivery and persistence, enable JetStream (already configured above):

const js = nc.jetstream();
const jsm = await nc.jetstreamManager();

await jsm.streams.add({ name: 'ORDERS', subjects: ['orders.*'] });

Monitoring NATS

curl http://localhost:8222/varz

The built-in HTTP monitoring endpoint exposes server statistics — restrict access to this port at the firewall level, same as any monitoring endpoint.

Common Errors

Connection refused — verify the firewall allows port 4222 from your application's IP.

Messages lost when subscriber wasn't connected — expected behavior for basic pub/sub without JetStream; use JetStream if delivery guarantees matter for your use case.

Best Practices

  • Enable authentication — never run NATS without it in production
  • Use JetStream when message durability matters
  • Restrict the monitoring HTTP port to trusted access only

Continue Reading

Browse more articles in Object Storage, Messaging & APIs.

  • nats, lightweight messaging, pub sub, jetstream
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Set Up Self-Hosted S3-Compatible Object Storage with MinIO

MinIO is a high-performance, self-hosted object storage server compatible with the S3 API —...

How to Use Object Storage for Application File Uploads

Storing user-uploaded files directly on your application server's disk creates scaling and...

How to Install and Configure RabbitMQ on a VPS

RabbitMQ is a widely-used, robust message broker — enabling applications to communicate...

How to Install and Configure Redis as a Message Queue

Redis, primarily known as a cache, also works well as a lightweight message queue for simpler use...

How to Build and Secure a REST API on a VPS

This guide covers the essential security and architecture practices for deploying a REST API on...