Skip to main content

Acknowledgments and errors

With the Socket.IO adapter, clients may attach an acknowledgment callback when emitting an event. A handler can respond by returning a value; the WebSocket result resolver passes that value to the acknowledgment callback.

@WebSocketEvent('message')
sendMessage(@WsData() data: { text: string }) {
return { accepted: true, text: data.text };
}

To control the acknowledgment directly, inject @WsAck(). The callback is optional because the client may emit without an acknowledgment:

@WebSocketEvent('message')
sendMessage(
@WsData() data: { text: string },
@WsAck() acknowledge: ((response?: unknown) => void) | undefined
) {
acknowledge?.({ accepted: true, text: data.text });
}

Use WebSocketResults to create a success or error result explicitly. WebSocketResults.Ok(value) acknowledges with value. WebSocketResults.Error(error) acknowledges with an object containing error, status, and an optional description.

import { WebSocketError, WebSocketResults } from '@axisparkjs/websocket';

@WebSocketEvent('message')
sendMessage(@WsData() data: { text: string }) {
if (!data.text.trim()) {
return WebSocketResults.Error(new WebSocketError('Message cannot be empty', 400, {
description: 'Provide a non-empty text value.'
}));
}

return WebSocketResults.Ok({ accepted: true });
}

If no acknowledgment callback was provided by the client, result processing has no callback to invoke. Returning undefined or null does not send a success acknowledgment.