
Deploy NATS Server with JetStream, WebSockets, and Flexible Authentication
This NATS Server is configured for production environments.
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
, andWS_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 Name | CLUSTER_ROUTES_SEED Setting |
---|---|
nats-ws | nats/localhost:6222 |
nats-srv-1 | nats://nats-ws.railway.internal:6222 |
nats-srv-2 | nats://nats-ws.railway.internal:6222 |
🔑 Configuration Variables (.env
)
The entrypoint forces Username/Password authentication to ensure security and privilege segregation.
JetStream and Server Configuration
Variable | Default | Description |
---|---|---|
SERVER_NAME | nats-server (Code) | Name of the NATS server instance. |
JETSTREAM_STORE | /data | Mount 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.
Variable | Description |
---|---|
MS_USER , MS_PASSWORD | Required Credentials for Microservices (TCP connection). They have total privileges (> ) for publish/subscribe. |
WS_USER , WS_PASSWORD | Required 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
.
Variable | Default | Description |
---|---|---|
ALLOWED_ORIGINS | Empty | Comma-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-wsMS_USER
User authentication tcp server
WS_USER
Username for the WebSocket connection
MS_PASSWORD
Password authentication tcp server
WS_PASSWORD
Password for the WebSocket connection
nats-server-2
Tyrcode/nats-wsMS_USER
User authentication tcp server
MS_PASSWORD
Password authentication tcp server
nats-server-1
Tyrcode/nats-wsMS_USER
User authentication tcp server
MS_PASSWORD
Password authentication tcp server