Skip to content

Commit 8ff8318

Browse files
Merge pull request #3340 from kim-sung-jee/kim-seong-jee-docs/websocket
docs(websockets): Add @ack() section
2 parents 03323c5 + 530e065 commit 8ff8318

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

content/websockets/gateways.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,28 @@ The `handleEvent()` method will be executed. In order to listen for messages emi
136136
socket.emit('events', { name: 'Nest' }, (data) => console.log(data));
137137
```
138138

139+
While returning a value from a message handler implicitly sends an acknowledgement, advanced scenarios often require direct control over the acknowledgement callback.
140+
141+
The `@Ack()` parameter decorator allows injecting the `ack` callback function directly into a message handler.
142+
Without using the decorator, this callback is passed as the third argument of the method.
143+
144+
```typescript
145+
@@filename(events.gateway)
146+
@SubscribeMessage('events')
147+
handleEvent(
148+
@MessageBody() data: string,
149+
@Ack() ack: (response: { status: string; data: string }) => void,
150+
) {
151+
ack({ status: 'received', data });
152+
}
153+
@@switch
154+
@Bind(MessageBody(), Ack())
155+
@SubscribeMessage('events')
156+
handleEvent(data, ack) {
157+
ack({ status: 'received', data });
158+
}
159+
```
160+
139161
#### Multiple responses
140162

141163
The acknowledgment is dispatched only once. Furthermore, it is not supported by native WebSockets implementation. To solve this limitation, you may return an object which consists of two properties. The `event` which is a name of the emitted event and the `data` that has to be forwarded to the client.

0 commit comments

Comments
 (0)