I have this function in javascript that given a durable consumer fetches messages and triggers a callback function:
export const consumeMessages = async (callback, consumer) => {
const iter = await consumer.fetch({ max_messages: 1 });
(async () => {
for await (const m of iter) {
await callback(m.subject, m.data);
m.ack()
}
})();
return iter
};
consumeMessages
is triggered once on booting up of my app:
await jsm.consumers.add(config.NATS_STREAM_NAME, {
ack_policy: AckPolicy.Explicit,
durable_name: sessionId,
filter_subject: subject,
});
const consumer = await js.consumers.get(config.NATS_STREAM_NAME, gameSession);
const iter = await consumeMessages(receiveMessages, consumer);
```
This works but only retrieves 1 message. I want to consumer to keep retrieving messages that come in.
I understand that this is happening because of this line `await consumer.fetch({ max_messages: 1 });`
But I cannot find another way to do this from the documentation. I do not want to hardcode a `mac_messages` value since I want consumer to keep fethching until it is deleted.