18 lines
424 B
TypeScript
18 lines
424 B
TypeScript
|
|
import { connect } from "nats";
|
||
|
|
|
||
|
|
const NATS_SERVER = process.env.NATS_SERVER || "nats://localhost:4222";
|
||
|
|
|
||
|
|
const nc = await connect({ servers: NATS_SERVER });
|
||
|
|
|
||
|
|
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()}`
|
||
|
|
);
|
||
|
|
}
|