robtaussig / react-use-websocket

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

Multiple Websocket connection ? #79

Open cubecleveland opened 3 years ago

cubecleveland commented 3 years ago

Hello, Not sure if this is an issue or a discussion or just not understangin the code.. :)

Looked at the code and it seems like every component is opening a new WebSocket connection via new WebSocket() I cant really spot the 'sharing mechanism' Can u assist ands let me know where in the code the compoenent actually shares the connection that akleady exist s?

Thanks R

robtaussig commented 3 years ago

Hi @cubecleveland,

Are you passing { share: true } in the option param?

https://github.com/robtaussig/react-use-websocket#share-boolean

cubecleveland commented 3 years ago

Hi!

Yes i solved it, i read through the code and i think the imlemetation is neat but the use case for me would not work in the way the socket was implemented and the share.

Mainly im looking for a function that will only update the socket every interval...... Only send the queeuu to the COmponeent every 10 seconds in an array of what was recieved in the last interval.

Reason is im dealing with 100's of events per seconds. Can that be acheived uasing the filter param ?

robtaussig commented 3 years ago

@cubecleveland

I'm glad you brought that up -- I have been meaning to implement a throttle param that does just that -- accept a duration, and batch messages in groups such that you only ever get one update per interval. I think I'll do that next!

I think using the filter param is interesting and probably more performant than my next suggestion, but it will require abusing the filter callback:) alternatively, you can wrap useWebsocket with your own custom hook that batches the messages, but I'm struggling to imagine whether this would actually solve the performance problem of rapid updates since your component is already being updated by the internal hook.

cubecleveland commented 3 years ago

Correct.

IMHO..... using the filter doesnt work and it will require some refactoring . Maybe adding Interval params to the option and justrun the filter on that interval... just a thought.... either way will be a great thing....

In the meantime, i just implemented my owb hook not using your library but i would love to once this feature is enabled...

Thanks so much for your great work!

krall12 commented 1 year ago

Not sure if related but is there a way to share connections between different paths on the same socket URL?

So imagine I had a /connect endpoint to establish the connection but also wanted to share it with a /file-upload endpoint on the same socket. Is that something that is possible?