Skip to content

Conversation

@Miodec
Copy link

@Miodec Miodec commented Dec 14, 2025

I noticed that the disconnect reason (https://socket.io/docs/v4/server-api#events-2) is not passed through to the onDisconnect hook. This PR fixes that.

Summary by CodeRabbit

  • Bug Fixes

    • Disconnect handling now captures and forwards the disconnect reason for improved diagnostics.
  • Breaking Changes

    • Custom disconnect handlers may need to accept an additional disconnect-reason field in their context.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 14, 2025

Walkthrough

Disconnect handling now forwards the disconnect reason: the disconnect handler constructs an augmented context by spreading the existing ctx and adding a reason (typed as DisconnectReason) and passes that to onDisconnect; the Hooks.onDisconnect type was updated to accept the augmented context.

Changes

Cohort / File(s) Summary
Disconnect handler implementation
src/attach.ts
Disconnect event handler now calls onDisconnect with an augmented context ({ ...ctx, reason }) instead of the plain ctx.
Public hooks type
src/hooks.ts
Hooks<E, D> updated: onDisconnect signature now expects ClientContext<E, D> & { reason: DisconnectReason } (imports DisconnectReason from socket.io).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Areas to check:
    • src/hooks.ts — confirm the DisconnectReason import and updated onDisconnect signature are intended.
    • src/attach.ts — verify reason is obtained and typed correctly when creating the augmented context.
    • Search for other onDisconnect implementations to confirm they accept the extended context or update them.

Poem

🐰 I hopped through sockets, soft and clear,
I brought a reason when goodbyes appear,
A tiny note for each farewell,
So every disconnect has its tell. 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Pass disconnect reason to the onDisconnect hook' is clear, concise, and directly summarizes the main change in the pull request.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Miodec Miodec marked this pull request as draft December 14, 2025 17:00
@Miodec Miodec marked this pull request as ready for review December 14, 2025 17:05
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/hooks.ts (1)

26-27: Consider logging the disconnect reason.

The default hook now receives the reason but doesn't log it. Including it could help with debugging disconnect issues.

-  onDisconnect: ({ client: { id, getData }, logger }) =>
-    logger.debug("Client disconnected", { ...getData(), id }),
+  onDisconnect: ({ client: { id, getData }, logger, reason }) =>
+    logger.debug("Client disconnected", { ...getData(), id, reason }),
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 626a3b1 and ce6afb5.

📒 Files selected for processing (2)
  • src/attach.ts (1 hunks)
  • src/hooks.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/hooks.ts (2)
src/handler.ts (2)
  • Handler (48-48)
  • ClientContext (24-28)
src/index.ts (1)
  • ClientContext (12-12)
🔇 Additional comments (1)
src/attach.ts (1)

121-121: LGTM! Correctly forwards the disconnect reason.

The implementation properly captures the reason from Socket.IO's disconnect event and passes it to the onDisconnect hook by spreading the existing context and adding the reason property. This aligns with the type definition in hooks.ts.

@RobinTail RobinTail added the enhancement New feature or request label Dec 16, 2025
@coveralls-official
Copy link

Coverage Status

coverage: 100.0%. remained the same
when pulling 89cb941 on Miodec:disconnect-reason
into ab4c68c on RobinTail:main.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/hooks.ts (2)

15-18: Type augmentation implemented correctly.

The intersection type properly extends ClientContext<E, D> with the reason: DisconnectReason field, enabling the disconnect handler to receive Socket.IO's disconnect reason.

Consider adding a JSDoc comment to document the new reason parameter for better developer experience:

 export interface Hooks<E extends EmissionMap, D extends z.ZodObject> {
   /** @desc The place for emitting events regardless receiving events */
   onConnection: Handler<ClientContext<E, D>, void>;
+  /** @desc Called when a client disconnects, with the disconnect reason provided by Socket.IO */
   onDisconnect: Handler<
     ClientContext<E, D> & { reason: DisconnectReason },
     void
   >;

30-31: Consider logging the disconnect reason for enhanced debugging.

The default implementation could be enhanced to include the disconnect reason in the log output, providing more context during debugging:

-  onDisconnect: ({ client: { id, getData }, logger }) =>
-    logger.debug("Client disconnected", { ...getData(), id }),
+  onDisconnect: ({ client: { id, getData }, logger, reason }) =>
+    logger.debug("Client disconnected", { ...getData(), id, reason }),
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c5d1eba and 89cb941.

📒 Files selected for processing (1)
  • src/hooks.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/hooks.ts (3)
src/emission.ts (1)
  • EmissionMap (15-15)
src/index.ts (2)
  • EmissionMap (10-10)
  • ClientContext (12-12)
src/handler.ts (2)
  • Handler (48-48)
  • ClientContext (24-28)
🔇 Additional comments (1)
src/hooks.ts (1)

10-10: LGTM! Previous review suggestions implemented correctly.

The type-only import of DisconnectReason from Socket.IO addresses both previous review comments and follows TypeScript best practices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants