feat(websockets): allow manual acknowledgement handling with @Ack() decorator#15543
Merged
kamilmysliwiec merged 5 commits intonestjs:masterfrom Oct 21, 2025
Merged
Conversation
Pull Request Test Coverage Report for Build fa491690-4869-4361-8a59-e7d591cea9dbDetails
💛 - Coveralls |
c2534b8 to
0c7b94d
Compare
ducbidapne
approved these changes
Aug 22, 2025
Hardanish-Singh
approved these changes
Sep 1, 2025
4 tasks
Member
|
Would you like to create a PR to the docs too? (to provide |
10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 implicitackhandling is great for simple, synchronous responses.However, there is no way for developers to get a direct reference to the
ackcallback function within the handler. This makes it very difficult or impossible to implement advanced use cases, such as:try...catchblock).This limitation is related to the issue described in #15286, where the
ackcallback 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 theackcallback 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 anackbased on thereturnvalue 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.ACKwas introduced.GatewayMetadataExplorernow detects the usage of@Ack()and adds anisAckHandledManuallyflag to the handler's metadata.WebSocketsControllercorrectly propagates this flag to the platform adapter.IoAdapternow consumes this flag to conditionally skip the automaticackcall.Does this PR introduce a breaking change?
Other information