Files
nats.js/jetstream/examples/js_readme_publish_examples.js
Alberto RicartandGitHub 63a17e9c71 [FIX] migrated 06_heartbeats.js to use the new event name as per jetstream migration docs (#182)
[CHANGE] changed the examples to be plain javascript, as this makes it easier to use out of the box.

Signed-off-by: Alberto Ricart <[email protected]>
2025-01-07 18:02:17 -06:00

62 lines
2.4 KiB
JavaScript

/*
* Copyright 2024 Synadia Communications, Inc
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { connect, Empty } from "@nats-io/transport-deno";
import { jetstream, jetstreamManager } from "@nats-io/jetstream";
const nc = await connect();
const jsm = await jetstreamManager(nc);
await jsm.streams.add({ name: "a", subjects: ["a.*"] });
// create a jetstream client:
const js = jetstream(nc);
// publish a message received by a stream
let pa = await js.publish("a.b");
// the jetstream returns an acknowledgement with the
// stream that captured the message, it's assigned sequence
// and whether the message is a duplicate.
console.log(
`stored in ${pa.stream} with sequence ${pa.seq} and is a duplicate? ${pa.duplicate}`,
);
// More interesting is the ability to prevent duplicates
// on messages that are stored in the server. If
// you assign a message ID, the server will keep looking
// for the same ID for a configured amount of time (within a
// configurable time window), and reject messages that
// have the same ID:
await js.publish("a.b", Empty, { msgID: "a" });
// you can also specify constraints that should be satisfied.
// For example, you can request the message to have as its
// last sequence before accepting the new message:
await js.publish("a.b", Empty, { expect: { lastMsgID: "a" } });
await js.publish("a.b", Empty, { expect: { lastSequence: 3 } });
// save the last sequence for this publish
pa = await js.publish("a.b", Empty, { expect: { streamName: "a" } });
// you can also mix the above combinations
// now if you have a stream with different subjects, you can also
// assert that the last recorded sequence on subject on the stream matches
const buf = [];
for (let i = 0; i < 100; i++) {
buf.push(js.publish("a.a", Empty));
}
await Promise.all(buf);
// if additional "a.b" has been recorded, this will fail
await js.publish("a.b", Empty, { expect: { lastSubjectSequence: pa.seq } });
await nc.drain();