Skip to content

Ack

reference
1 min readUpdated

Kind: Function

Source: packages/websockets/decorators/ack.decorator.ts

Part of: Websockets

WebSockets ack parameter decorator. Extracts the ack callback function from the arguments of a ws event.

This decorator signals to the framework that the ack callback will be handled manually within the method, preventing the framework from automatically sending an acknowledgement based on the return value.

Ack is a WebSocket parameter decorator that injects the event acknowledgement callback into a handler method. Using this decorator tells the framework that the handler will send its own acknowledgement response instead of automatically acknowledging the event from the method’s return value.

Signature

ts
function Ack(): ParameterDecorator

Returns: ParameterDecorator

Diagram

mermaid
graph LR
  Client[WebSocket client] -->|emit event with ack callback| Gateway[Gateway event handler]
  Gateway -->|@Ack()| AckParam[ack callback parameter]
  AckParam -->|manual acknowledgement| Client
  Gateway -. return value is not auto-acknowledged .-> Framework[WebSocket framework]

Usage

ts
import { Ack, SubscribeMessage } from '@nestjs/websockets';

export class ChatGateway {
  @SubscribeMessage('send-message')
  handleMessage(
    @Ack() ack: (response: { ok: boolean; messageId?: string }) => void,
  ) {
    const messageId = crypto.randomUUID();

    // Perform application work before responding.
    ack({
      ok: true,
      messageId,
    });
  }
}

AI Coding Instructions

  • Use @Ack() only on WebSocket event handler parameters where the acknowledgement must be sent manually.
  • Call the injected ack callback with the response payload expected by the connected client.
  • Do not rely on the handler return value to acknowledge an event when @Ack() is present.
  • Keep acknowledgement handling inside the handler or delegate it to a service, ensuring the callback is invoked after the relevant async work completes.
  • Ensure client-side event emissions provide an acknowledgement callback when the server handler expects one.

Used by

1 reference from 1 file. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (1)

  • AckGatewayintegration/websockets/src/ack.gateway.ts:8

Was this page helpful?

Download as PDF
Ack — NestJS head-to-head