nats-example/index.ts

22 lines
600 B
TypeScript
Raw Normal View History

2025-08-04 10:14:50 +02:00
import { connect } from "nats";
const NATS_SERVER = process.env.NATS_SERVER || "nats://localhost:4222";
2025-08-04 10:53:07 +02:00
const NATS_AUTH_TOKEN = process.env.NATS_AUTH_TOKEN || "s3cr3t";
2025-08-04 10:14:50 +02:00
2025-08-04 10:53:07 +02:00
console.log(
`Connecting to NATS at ${NATS_SERVER} with token ${NATS_AUTH_TOKEN}`
);
2025-08-04 10:14:50 +02:00
2025-08-04 10:53:07 +02:00
const nc = await connect({ servers: NATS_SERVER, token: NATS_AUTH_TOKEN });
2025-08-04 10:14:50 +02:00
console.log("Connected to NATS");
const sub = nc.subscribe("test");
console.log("Subscribed to [test], waiting for messages...\n");
for await (const msg of sub) {
console.log(
`Received message [${msg.subject} - ${msg.sid}]: ${msg.data.toString()}`
);
}