feat(websockets): allow manual acknowledgement handling with @Ack() decorator #15543
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Currently, NestJS's WebSocket implementation provides a convenient way to handle Socket.IO acknowledgements by returning a value from a
@SubscribeMessage()
handler. This implicitack
handling is great for simple, synchronous responses.However, there is no way for developers to get a direct reference to the
ack
callback function within the handler. This makes it very difficult or impossible to implement advanced use cases, such as:try...catch
block).This limitation is related to the issue described in #15286, where the
ack
callback is not correctly passed to the handler if declared as a parameter.Issue Number: #15286
What is the new behavior?
This PR introduces a new
@Ack()
parameter decorator and enhances the underlying WebSocket infrastructure to allow for explicit, manual handling of acknowledgement callbacks, bringing it in line with the capabilities ofsocket.io
.New
@Ack()
Decorator: Developers can now inject theack
callback function directly into their@SubscribeMessage()
handlers, similar to how@MessageBody()
or@ConnectedSocket()
work.Automatic Ack Deactivation: When the
@Ack()
decorator is used in a handler, the framework's default behavior of automatically sending anack
based on thereturn
value is disabled. This prevents accidental double-sends and provides clear, predictable behavior.Preserved Implicit Behavior: Handlers that do not use the
@Ack()
decorator will continue to function as before. Returning a value will still trigger an implicit acknowledgement, ensuring full backward compatibility.To achieve this, the following changes were made:
WsParamtype.ACK
was introduced.GatewayMetadataExplorer
now detects the usage of@Ack()
and adds anisAckHandledManually
flag to the handler's metadata.WebSocketsController
correctly propagates this flag to the platform adapter.IoAdapter
now consumes this flag to conditionally skip the automaticack
call.Does this PR introduce a breaking change?
Other information