SvenKirschbaum / react-stomp-hooks

This repository contain a react library which enables simple access to stomp subscriptions via hooks.
MIT License
59 stars 12 forks source link

need for a custom unsubscribe #7

Closed manemao closed 2 years ago

manemao commented 2 years ago

besides when "parent component is unmounting" there is situations that you need to unsubscribe a specific destination. i didn't find fancy way to do so with this library. one clean way, for example, could be returning an array of subscription objects as useSubscription hook output.

or maybe there is a clean way to do it and it just not well documented. thanks anyway for lib 👍

manemao commented 2 years ago

Although there is a way for custom unsubscription with useStompClient(): const sub = useStompClient().subscribe('/...', callback) sub.unsubscribe()

didn't knew that when submitting this issue, closing it now.

SvenKirschbaum commented 2 years ago

Hello @manemao!

The preferred way to do this would be in a declarative way, by changing the list of topics you want to subscribe to provided to the useSubscription hook. I just tested this and discovered a bug preventing this use case due to an oversight on my end. I published a fix for this, and included an additional example showing how you can dynamically subscribe/unsubscibe:

https://github.com/fallobst22/react-stomp-hooks/blob/170c9b47433612c3e7fe31e0ebbf02078e647419/example/src/App.js#L154-L172

The way you describe in your comment will work absolutely fine too, if you want to achive this in an imperative way. Note however that you should only use this inside an effect and you are responsible for correctly calling the cleanup function yourself too.

manemao commented 2 years ago

Hi @fallobst22 ! thanks for immediate fix and comment on the issue 🙏