VictorHAS / mqtt-react-hooks

ReactJS library for Pub/Sub communication with an MQTT broker using Hooks
MIT License
91 stars 28 forks source link

is it a problem since it never unsubscribe subscribed message? thanks. #34

Open postgetme opened 3 years ago

postgetme commented 3 years ago

for example I have 2 pages which with different subscriptions, I open page 1 then open page 2, in the page 2 will receive all message on the bottom.

NDallaRiva commented 2 years ago

The page 2 will receive also the messages of page 1 subscription.

I think this is not the best behavior. This can be improved adding the unsubscribe action In useSubscription.tsx in line 36 we have the useEffect hook `useEffect(() => { if (client?.connected) { subscribe();

  client.on('message', callback);
}
return () => {
  client?.off('message', callback);
  // Add unsubscription here, something like
 client?.unsubscribe(topic)
};

}, [callback, client, subscribe]);`

sfeu commented 2 years ago

If individual react components are subscribing ("useSubscription") for specific topics and later on are longer rendered - it would be helpful to support an unsubscribe option to reduce bandwidth.