mirror of
https://github.com/hanzomsg/nats.js.git
synced 2026-10-30 20:31:36 +00:00
[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]>
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
/*
|
|
* Copyright 2023 The NATS Authors
|
|
* 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 { nuid } from "@nats-io/nats-core";
|
|
import { AckPolicy, jetstreamManager } from "@nats-io/jetstream";
|
|
|
|
export async function setupStreamAndConsumer(
|
|
nc,
|
|
messages = 100,
|
|
) {
|
|
const stream = nuid.next();
|
|
const jsm = await jetstreamManager(nc);
|
|
const js = jsm.jetstream();
|
|
await jsm.streams.add({ name: stream, subjects: [`${stream}.*`] });
|
|
const buf = [];
|
|
for (let i = 0; i < messages; i++) {
|
|
buf.push(js.publish(`${stream}.${i}`, `${i}`));
|
|
if (buf.length % 500 === 0) {
|
|
await Promise.all(buf);
|
|
buf.length = 0;
|
|
}
|
|
}
|
|
if (buf.length > 0) {
|
|
await Promise.all(buf);
|
|
buf.length = 0;
|
|
}
|
|
|
|
const consumer = await jsm.consumers.add(stream, {
|
|
name: stream,
|
|
ack_policy: AckPolicy.Explicit,
|
|
});
|
|
|
|
return { stream, consumer };
|
|
}
|