Deploy NATS Server with JetStream, WebSockets, and Flexible Authentication

This NATS Server is configured for production environments.

Deploy NATS Server with JetStream, WebSockets, and Flexible Authentication

nats-seed-ws

Tyrcode/nats-ws

Just deployed

/nats

nats-server-2

Tyrcode/nats-ws

Just deployed

/nats

nats-server-1

Tyrcode/nats-ws

Just deployed

/nats

Deploy and Host NATS Server Cluster on Railway

What is NATS Server Cluster with JetStream and Granular Authentication?

NATS is a high-performance, open-source messaging system designed for cloud-native applications. This project is a production-ready Docker image that bundles Core NATS, JetStream (for durable messaging and streaming), and a segregated WebSockets endpoint. It provides a robust, production-ready environment engineered for high-scale, low-latency microservice and client communication, fully configured via environment variables and enforcing Username/Password authentication for granular security.

About Hosting NATS Server Cluster

Hosting this NATS container on Railway simplifies security by utilizing the platform's TLS/SSL proxy termination, allowing NATS to run internally without managing certificates. Achieving high availability requires creating a cluster of at least three nodes. One node uses the ws/Dockerfile (WebSockets enabled), and two use the server/Dockerfile (WebSockets disabled). A persistent volume is mandatory for the /data directory to ensure JetStream's data durability.

Common Use Cases

  • Real-Time Data Streaming: Deploying JetStream for reliable event sourcing, command logs, and durable queues in critical systems like financial trading or IoT sensor aggregation.

  • Decoupled Microservice Communication: Using Core NATS for asynchronous, high-speed message passing between backend services, significantly enhancing system resilience and scalability.

  • Live User Experience (UX) Updates: Utilizing the WebSockets endpoint (on the designated WS node) to deliver real-time notifications, chat messages, or live progress indicators directly to browser and mobile clients.

Dependencies for NATS Server Cluster Hosting

Persistent Volume: Mandatory for the /data directory to ensure JetStream's stream and message persistence is maintained across service restarts.

Deployment Dependencies

  • Generate Credentials (All four environment variables for MS_USER, MS_PASSWORD, WS_USER, and WS_PASSWORD must be defined).

Why Deploy NATS Server Cluster on Railway?

Cluster Configuration on Railway

To create a cluster of NATS servers, you must set the CLUSTER_ROUTES_SEED environment variable using the internal URLs provided by Railway for the other service replicas. All nodes (WS and Server) will point to the primary WS node for initial peer discovery.

If you have three NATS replicas named nats-ws (the seed), nats-srv-1, and nats-srv-2:

Service NameCLUSTER_ROUTES_SEED Setting
nats-wsnats/localhost:6222
nats-srv-1nats://nats-ws.railway.internal:6222
nats-srv-2nats://nats-ws.railway.internal:6222

🔑 Configuration Variables (.env)

The entrypoint forces Username/Password authentication to ensure security and privilege segregation.

JetStream and Server Configuration

VariableDefaultDescription
SERVER_NAMEnats-server (Code)Name of the NATS server instance.
JETSTREAM_STORE/dataMount directory for JetStream persistence. Must be a Docker volume.

Authentication Configuration (Mandatory Granular Mode)

The server is configured exclusively for Username/Password authentication, forcing privilege segregation between use cases.

VariableDescription
MS_USER, MS_PASSWORDRequired Credentials for Microservices (TCP connection). They have total privileges (>) for publish/subscribe.
WS_USER, WS_PASSWORDRequired Credentials for Web Clients (Used by the WS node). They have restricted privileges (ws-app.>).

WebSocket Configuration (Only for the WS Node)

These variables are only relevant for the node running the ws/Dockerfile.

VariableDefaultDescription
ALLOWED_ORIGINSEmptyComma-separated list of WEB domains (e.g., https://app.com,https://test.com). Necessary for CORS in browsers. Only for web apps.

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying the NATS Server Cluster on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.


Template Content

nats-seed-ws

Tyrcode/nats-ws

nats-server-2

Tyrcode/nats-ws

nats-server-1

Tyrcode/nats-ws

More templates in this category

View Template
smoothmq
A drop-in replacement for AWS SQS

View Template
Redpanda+Console : 1$
Enterprise-Grade Redpanda Streaming Platform

View Template
Maildev
Ephemeral SMTP server for development purposes.