robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.63k stars 135 forks source link

Shared websocketRef points to old connection after reconnecting. #108

Closed julianwachholz closed 3 years ago

julianwachholz commented 3 years ago

In the event of the server terminating the connection the hook will attempt to reconnect.

Shared connections however fail to update the Ref and will point to the old connection which now has a readyState of CLOSED.

So using sendMessage will silently fail and put the message in a queue that is never read.

https://github.com/robtaussig/react-use-websocket/blob/5f337b8da99293a43329e2d7ab9c7b5206bce5b4/src/lib/use-websocket.ts#L58

Minimal example: https://codesandbox.io/embed/stoic-leftpad-o0x0g?fontsize=14&hidenavigation=1&theme=dark

For the example to show the error we need to run it locally and kill the server once it's connected and restart it.

robtaussig commented 3 years ago

Thank you for the report! Fixed in d0d2eb8e12616b69541d960a3753618b176f132b and published to 2.9.1. Will explain details in this https://github.com/robtaussig/react-use-websocket/issues/90.

xieqingtian commented 2 years ago

looks like it not be fixed.

websocketRef of other shared subscribers should be synchronized after reconnection.