const { connect, JSONCodec } = require("nats"); async function main(count = 100){ const nc = await connect(['nats://localhost:4222']); console.log("NATS connected"); const jsm = await nc.jetstreamManager(); // Delete an existing stream if required //await jsm.streams.delete("results"); await jsm.streams.add({ name: "results", subjects: ["results.topic.io"] }); // create a jetstream client: const js = nc.jetstream(); const jc = new JSONCodec(); // to publish messages to a stream: for (var i = 0; i< count; i++){ let pa = await js.publish("results.topic.io", jc.encode({ hello: "world", index: i})); //Alternatively you could also do a simple publish via nats //await nc.publish("results.iotify.io", jc.encode({ hello: "world", index: i})); } console.log("Published", count); } try{ main(process.argv[2]); } catch(e){ console.error(e); }