Skip to content

fix(useWebSocket): check the current socket before sending - #2995

Open
Aminark wants to merge 2 commits into
alibaba:masterfrom
Aminark:fix/websocket-send-current-state
Open

Aminark wants to merge 2 commits into
alibaba:masterfrom
Aminark:fix/websocket-send-current-state

Conversation

@Aminark

@Aminark Aminark commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

🤔 This is a ...

  • Bug fix
  • Test Case

🔗 Related issue link

No linked issue. Reproduced with the regression tests in this PR.

💡 Background and solution

Sending an initial subscription from onOpen fails because the hook checks the ready state from the previous React render. The same stale state also lets a send immediately after disconnect() silently return, or sends to a replacement socket while it is still connecting.

Read the current socket and its native readyState when sending. Tests cover initial messages from onOpen, immediate disconnects, and reconnects before the replacement socket opens.

Validation:

  • Regression cases fail against the unchanged implementation and pass with this fix.
  • vitest run src/useWebSocket (8 tests).
  • tsc --noEmit passes for the hooks package.

📝 Changelog

Language Changelog
🇺🇸 English Use the current WebSocket state when sending messages, including from onOpen and during reconnects.
🇨🇳 Chinese 修复 useWebSocket 在 onOpen、断开连接和重连过程中使用过期状态发送消息的问题。

☑️ Self Check before Merge

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@pkg-pr-new

pkg-pr-new Bot commented Oct 29, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/ahooks@2995
npm i https://pkg.pr.new/@ahooks.js/use-url-state@2995

commit: 5a38a24

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant