Auto Ack
Automatically acknowledge received KubeMQ queue messages in Node.js so messages are removed on delivery without manual ack calls.
Overview
Auto-ack is the fire-and-forget receive mode for queues: the broker marks a message as consumed the instant it hands it to your client, instead of waiting for your code to settle it. Reach for it when the work is idempotent, low-value, or cheap to lose — a metrics ping, a cache warm, a best-effort notification — and you'd rather not carry the bookkeeping of explicit acknowledgment for every message.
It works by setting autoAck: true on the options passed to streamQueueMessages(). With it enabled, delivery and acknowledgment happen as one atomic step on the broker side, so there's no separate per-message ack() call and no in-flight "pending" state for the message to sit in.
Gotchas: if your consumer crashes or throws inside onMessages before it finishes processing a batch, those messages are gone for good — auto-ack gives you no chance to nack or requeue them, unlike Ack & Reject. It's an at-most-once model, so never use it for messages where losing one silently would matter. And because acknowledgment happens on delivery, maxMessages is your only throttle — there's no visibility-timeout window to tune.
Prerequisites
- KubeMQ server running on
localhost:50000 - Node.js SDK installed (
npm install kubemq-js)
Code
import { KubeMQClient, createQueueMessage } from 'kubemq-js';
async function main() {
const client = await KubeMQClient.create({
address: 'localhost:50000',
clientId: 'js-queues-stream-auto-ack-client',
});
try {
for (let i = 1; i <= 3; i++) {
await client.sendQueueMessage(
createQueueMessage({ channel: 'js-queues-stream.auto-ack', body: `auto-${i}` }),
);
}
const handle = client.streamQueueMessages({
channel: 'js-queues-stream.auto-ack',
autoAck: true,
maxMessages: 3,
});
handle.onMessages((msgs) => {
for (const m of msgs) {
console.log('Auto-acked:', new TextDecoder().decode(m.body));
}
handle.close();
});
handle.onError((err) => {
console.error('Error:', err.message);
});
await new Promise((r) => setTimeout(r, 2000));
} finally {
await client.close();
}
}
main().catch(console.error);
How It Works
autoAck: trueinstreamQueueMessages()instructs the server to acknowledge messages automatically as they are delivered — no per-messageack()call is needed.- This is the simplest consumption mode: messages are removed from the queue on delivery. If the client crashes after receiving but before processing, the messages are lost.
- Use auto-ack only when at-least-once delivery guarantees are not required. For processing-safe consumption, omit
autoAckand callmsg.ack()after successful processing. handle.close()is called insideonMessagesto end the stream once the batch is processed; the outersetTimeoutis a safety net if the stream never fires.
Related
Was this page helpful?