Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Deno] Example for List subjects for a specific stream #238

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions examples/jetstream/list-subjects/deno/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {
connect,
nuid
} from "https://deno.land/x/[email protected]/src/mod.ts";

const servers = Deno.env.get("NATS_URL")?.split(",");

const nc = await connect({ servers });

const jsm = await nc.jetstreamManager();
const js = nc.jetstream();

// Create a stream with a few random subjects
const plainSubj = `PLAIN_${nuid.next().substring(18)}`;
const gtSubj = `GT_${nuid.next().substring(18)}`;
const starSubj = `STAR_${nuid.next().substring(18)}`;
const name = `EVENTS_${nuid.next().substring(18)}`;

let si = await jsm.streams.add({
name,
subjects: [plainSubj, `${gtSubj}.>`, `${starSubj}.*`]
});

console.log(`Stream: ${si.config.name} has these subjects: '${si.config.subjects}'`)

// ### Get stream info with StreamInfoRequestOptions
// Get the subjects via the streams.info call.
// Since this is "state" there are no subjects in the state unless
// there are messages in the subject.
si = await jsm.streams.info(name, {subjects_filter: ">"});

const count = si.state.subjects
? Object.getOwnPropertyNames(si.state.subjects).length
: 0;
console.log(`Before publishing any messages, there should be 0 subjects in the state: ${count}`)

// console.log(`Before publishing any messages, there are 0 subjects: ${si.state.num_subjects}`)

// Publish a message
await js.publish(plainSubj)

si = await jsm.streams.info(name, {subjects_filter: ">"});
console.log("After publishing a message to a subject, it appears in state:")

if (si.state.subjects) {
for (const [key, value] of Object.entries(si.state.subjects)) {
console.log(` subject '${key}' has ${value} message(s)`)
}
}

// Publish some more messages, this time against wildcard subjects
await js.publish(`${gtSubj}.A`, "gtA-1");
await js.publish(`${gtSubj}.A`, "gtA-2");
await js.publish(`${gtSubj}.A.B`, "gtAB-1");
await js.publish(`${gtSubj}.A.B`, "gtAB-2");
await js.publish(`${gtSubj}.A.B.C`, "gtABC");
await js.publish(`${gtSubj}.B.B.B`, "gtBBB");
await js.publish(`${starSubj}.1`, "star1-1");
await js.publish(`${starSubj}.1`, "star1-2");
await js.publish(`${starSubj}.2`, "star2");

// Get all subjects
si = await jsm.streams.info(name, {subjects_filter: ">"});
console.log("Wildcard subjects show the actual subject, not the template.");

if (si.state.subjects) {
for (const [key, value] of Object.entries(si.state.subjects)) {
console.log(` subject '${key}' has ${value} message(s)`)
}
}

// ### Subject Filtering
// Instead of allSubjects, you can filter for a specific subject
si = await jsm.streams.info(name, {subjects_filter: `${gtSubj}.>`});
console.log(`Filtering the subject returns only matching entries ['${gtSubj}.>']`);

if (si.state.subjects) {
for (const [key, value] of Object.entries(si.state.subjects)) {
console.log(` subject '${key}' has ${value} message(s)`)
}
}

si = await jsm.streams.info(name, {subjects_filter: `${gtSubj}.A.>`});
console.log(`Filtering the subject returns only matching entries ['${gtSubj}.A>']`);

if (si.state.subjects) {
for (const [key, value] of Object.entries(si.state.subjects)) {
console.log(` subject '${key}' has ${value} message(s)`)
}
}

await nc.close();
Loading