robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.6k stars 136 forks source link

What do shouldConnect & protocols do? #195

Closed zehawki closed 1 year ago

zehawki commented 1 year ago

Documentation does not give any details on shouldConnect and protocols, and how they are supposed to be used.

I'm guessing shouldConnect is a way to manually disconnect the websocket based on this line, where it seems the "third param" is shouldConnect:

Components can close/unsubscribe from a WebSocket by passing false as the third parameter.

Will be good to have this documented in the Options section.

Some minor feedback:

  1. The Options section is a needless repeat of Interface, can be removed or options ordering harmonized with Interface section
zehawki commented 1 year ago

Hmm, this:

Components can close/unsubscribe from a WebSocket by passing false as the third parameter. This provides a more explicit solution than the previous method of setting the socketUrl to null. Both methods work and are supported usage.

is not working as expected.

Whether I set shouldConnect to false, or setSocketUrl(null) - the socket does not actually get closed.

So what should I do to manually close the socket?

robtaussig commented 1 year ago

I would need more details to diagnose what you are experiencing. A basic example of using the third parameter is working in a shared setting (open dev tools and inspect network tab and you can observe when the websocket transitions from pending state to closed): https://stackblitz.com/edit/react-duw2wn?file=index.js