Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(websockets): add disconnect reason parameter to OnGatewayDisconnect#15451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
snowykte0426 wants to merge3 commits intonestjs:master
base:master
Choose a base branch
Loading
fromsnowykte0426:master

Conversation

@snowykte0426
Copy link

@snowykte0426snowykte0426 commentedJul 24, 2025
edited by kamilmysliwiec
Loading

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Currently, when a WebSocket client disconnects, the handleDisconnect
method only receives the client object without any information about why
the disconnection occurred. This makes it difficult for developers to
implement different handling logic based on the disconnect reason (e.g.,
client-initiated vs. network error vs. timeout).

Issue Number:#15437

What is the new behavior?

This PR enhances the WebSocket disconnect handling by providing the
disconnect reason as an optional second parameter to the handleDisconnect
method.

Changes:

  • OnGatewayDisconnect Interface: Added optional reason?: string parameter
    to handleDisconnect method
  • NestGateway Interface: Updated to support the new disconnect reason
    parameter
  • WebSocketsController: Modified to capture and forward disconnect reason
    from events
  • IoAdapter: Enhanced to extract disconnect reason from Socket.IO
    disconnect events

Usage:

Before(stillworks):  @WebSocketGateway()exportclassChatGatewayimplementsOnGatewayDisconnect{handleDisconnect(client:Socket){console.log('Client disconnected:',client.id);}}

After (new feature):

  @WebSocketGateway()exportclassChatGatewayimplementsOnGatewayDisconnect{handleDisconnect(client:Socket,reason?:string){console.log('Client disconnected:',client.id,'Reason:',reason);// Handle different disconnect scenariosswitch(reason){case'client namespace disconnect':// Client explicitly disconnectedbreak;case'transport close':// Connection lost due to network issuesbreak;case'ping timeout':// Client didn't respond to pingbreak;}}}

Common Disconnect Reasons:

  • 'client namespace disconnect' - Client explicitly called disconnect()
  • 'server namespace disconnect'- Server forced disconnection
  • 'transport close'- Underlying transport was closed
  • 'transport error' - Transport encountered an error
  • 'ping timeout' - Client didn't respond to ping in time

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Implementation Details:

The implementation modifies the following core components:

  1. Interface Updates: Both OnGatewayDisconnect and NestGateway interfaces
    now support the optional reason parameter
  2. Event Flow: The disconnect reason flows from Socket.IO → IoAdapter →
    WebSocketsController → Gateway
  3. Backward Compatibility: The implementation checks for data format and
    handles both old and new formats seamlessly
  4. Fix Applied: Corrected distinctUntilChanged() operator behavior to
    properly handle both old format (just client) and new format ({ client, reason }) for disconnect events

yizumi1012xxx reacted with heart emoji
@coveralls
Copy link

coveralls commentedJul 24, 2025
edited
Loading

Pull Request Test Coverage Report forBuild ccc32411-c048-48b5-946a-1ddf86b11307

Details

  • 3 of7(42.86%) changed or added relevant lines in1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to88.885%

Changes Missing CoverageCovered LinesChanged/Added Lines%
packages/websockets/web-sockets-controller.ts3742.86%
TotalsCoverage Status
Change from baseBuild a2f4d25f-1d25-415f-b2c5-967298b635ae:-0.03%
Covered Lines:7285
Relevant Lines:8196

💛 -Coveralls

This change enhances the WebSocket disconnect handling by providingthe disconnect reason as an optional second parameter to thehandleDisconnect method.Changes:- Add optional reason parameter to OnGatewayDisconnect interface- Update NestGateway interface to support disconnect reason- Modify WebSocketsController to capture and forward disconnect reason- Enhance IoAdapter to extract reason from Socket.IO disconnect events- Maintain full backward compatibility with existing implementations- Add comprehensive unit and integration testsThe disconnect reason helps developers understand why clients disconnect,enabling better error handling and debugging. Common reasons include'client namespace disconnect', 'transport close', 'ping timeout', etc.This change is fully backward compatible - existing code continues towork without modification while new code can optionally access thedisconnect reason.Closesnestjs#15437Signed-off-by: snowykte0426 <snowykte0426@naver.com>
Comment on lines 38 to 43
constfilters=this.filters.filter(
filter=>filter.exceptionMetatypes?.length===0,
);
if(filters.length>0){
returnfilters[0].func(exception,host);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

why(?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This change prioritizes global exception filters (i.e., filters with empty exceptionMetatypes) over more specific ones. Previously, global filters that were intended to catch all exceptions could be skipped because they were processed after specific filters.

The updated logic ensures that if a filter is designed to handle all exception types (as indicated by an empty exceptionMetatypes array), it is invoked first, preserving the intended filter hierarchy and behavior.

Please let me know if I’ve misunderstood any part of this change.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Previously, global filters that were intended to catch all exceptions could be skipped because they were processed after specific filters.

That's the expected behavior.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks for the clarification! I've removed the exception filter changes
in commit6cf7ad5 since the current behavior is indeed the intended
behavior.

event
.pipe(distinctUntilChanged())
.subscribe(instance.handleDisconnect.bind(instance));
event.pipe(distinctUntilChanged()).subscribe((data:any)=>{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

dont thinkdistinctUntilChanged will work as expected now

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You’re right — thank you for pointing that out.
Since we’re now emitting objects like{ client, reason } instead of just the client,distinctUntilChanged() was comparing object references, which caused the deduplication to fail.

I’ve updated the code to include a custom comparison function fordistinctUntilChanged(). This handles both the previous format (just client) and the new one ({ client, reason }) by extracting and comparing the actual client values.

Really appreciate you catching this.

be6b50c

Fix the distinctUntilChanged operator in subscribeDisconnectEvent to properlycompare client objects when using the new { client, reason } format. Theprevious implementation would not deduplicate correctly as it compared objectreferences instead of the actual client instances.This ensures backward compatibility while properly handling both the oldformat (just client) and new format ({ client, reason }) for disconnect events.Signed-off-by: snowykte0426 <snowykte0426@naver.com>
Remove the changes to exception filter handling in RPC exceptions handleras the current behavior is the intended behavior according to maintainerfeedback.Signed-off-by: snowykte0426 <snowykte0426@naver.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@kamilmysliwieckamilmysliwieckamilmysliwiec left review comments

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@snowykte0426@coveralls@kamilmysliwiec

[8]ページ先頭

©2009-2025 Movatter.jp