This commit is contained in:
Nicola Zambello 2025-08-04 10:53:07 +02:00
parent e5553e56fb
commit a8ef4a2bad
Signed by: nzambello
GPG key ID: 0A7E9D12831FAAF9
4 changed files with 22 additions and 9 deletions

View file

@ -18,7 +18,7 @@ A simple NATS client/server setup for testing message publishing and subscribing
### 1. Start the NATS server and message subscriber ### 1. Start the NATS server and message subscriber
```bash ```bash
docker compose up nats server docker compose up --build nats server
``` ```
This starts: This starts:
@ -29,7 +29,7 @@ This starts:
### 2. Run the interactive client ### 2. Run the interactive client
```bash ```bash
docker compose run --rm client docker compose run --rm --build client
``` ```
This opens an interactive prompt where you can: This opens an interactive prompt where you can:
@ -57,13 +57,13 @@ docker compose down
1. **Terminal 1**: Start the infrastructure 1. **Terminal 1**: Start the infrastructure
```bash ```bash
docker compose up nats server -d docker compose up --build nats server
``` ```
2. **Terminal 2**: Run the client 2. **Terminal 2**: Run the client
```bash ```bash
docker compose run --rm client docker compose run --rm --build client
``` ```
You'll see: You'll see:
@ -94,7 +94,7 @@ bun install
### Start NATS server ### Start NATS server
```bash ```bash
docker run -p 4222:4222 nats:latest docker run -p 4222:4222 -ti nats:latest --auth s3cr3t
``` ```
### Run the server (in one terminal) ### Run the server (in one terminal)
@ -112,6 +112,7 @@ bun run client.ts
## Environment Variables ## Environment Variables
- `NATS_SERVER`: NATS server URL (default: `nats://localhost:4222`) - `NATS_SERVER`: NATS server URL (default: `nats://localhost:4222`)
- `NATS_AUTH_TOKEN`: NATS server [auth token](https://docs.nats.io/running-a-nats-service/configuration/securing_nats/auth_intro/tokens)
## Troubleshooting ## Troubleshooting

View file

@ -2,9 +2,14 @@ import { connect } from "nats";
import readline from "readline"; import readline from "readline";
const NATS_SERVER = process.env.NATS_SERVER || "nats://localhost:4222"; const NATS_SERVER = process.env.NATS_SERVER || "nats://localhost:4222";
const NATS_AUTH_TOKEN = process.env.NATS_AUTH_TOKEN || "s3cr3t";
const nc = await connect({ servers: NATS_SERVER }); console.log(
console.log(`Connected to NATS at ${NATS_SERVER}`); `Connecting to NATS at ${NATS_SERVER} with token ${NATS_AUTH_TOKEN}`
);
const nc = await connect({ servers: NATS_SERVER, token: NATS_AUTH_TOKEN });
console.log(`Connected to NATS`);
const rl = readline.createInterface({ const rl = readline.createInterface({
input: process.stdin, input: process.stdin,

View file

@ -1,8 +1,9 @@
services: services:
nats: nats:
# docker run -p 4222:4222 -ti nats:latest # docker run -p 4222:4222 -ti nats:latest --auth s3cr3t
image: nats:latest image: nats:latest
container_name: nats container_name: nats
command: "--auth s3cr3t"
ports: ports:
- 4222:4222 - 4222:4222
@ -15,6 +16,7 @@ services:
- nats - nats
environment: environment:
- NATS_SERVER=nats://nats:4222 - NATS_SERVER=nats://nats:4222
- NATS_AUTH_TOKEN=s3cr3t
client: client:
build: build:
@ -27,3 +29,4 @@ services:
- nats - nats
environment: environment:
- NATS_SERVER=nats://nats:4222 - NATS_SERVER=nats://nats:4222
- NATS_AUTH_TOKEN=s3cr3t

View file

@ -1,9 +1,13 @@
import { connect } from "nats"; import { connect } from "nats";
const NATS_SERVER = process.env.NATS_SERVER || "nats://localhost:4222"; const NATS_SERVER = process.env.NATS_SERVER || "nats://localhost:4222";
const NATS_AUTH_TOKEN = process.env.NATS_AUTH_TOKEN || "s3cr3t";
const nc = await connect({ servers: NATS_SERVER }); console.log(
`Connecting to NATS at ${NATS_SERVER} with token ${NATS_AUTH_TOKEN}`
);
const nc = await connect({ servers: NATS_SERVER, token: NATS_AUTH_TOKEN });
console.log("Connected to NATS"); console.log("Connected to NATS");
const sub = nc.subscribe("test"); const sub = nc.subscribe("test");