# Auto Ack (/sdks/nodejs/how-to/queues/auto-ack)



## Overview [#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](/sdks/nodejs/how-to/queues/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 [#prerequisites]

* KubeMQ server running on `localhost:50000`
* Node.js SDK installed (`npm install kubemq-js`)

## Code [#code]

```typescript title="auto-ack.ts"
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 [#how-it-works]

* `autoAck: true` in `streamQueueMessages()` instructs the server to acknowledge messages automatically as they are delivered — no per-message `ack()` 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 `autoAck` and call `msg.ack()` after successful processing.
* `handle.close()` is called inside `onMessages` to end the stream once the batch is processed; the outer `setTimeout` is a safety net if the stream never fires.

## Related [#related]

* [Pattern overview](/learn/queues/getting-started)
* [Node.js SDK Reference](/sdks/nodejs/reference)
* [Send & Receive](/sdks/nodejs/tutorials/send-receive)
* [Ack All](/sdks/nodejs/how-to/queues/ack-all)
