Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count subscriptions and make unsubscribe only stop if it matches the amount of subscriptions #1959

Closed
dzek69 opened this issue Dec 28, 2024 · 6 comments

Comments

@dzek69
Copy link

dzek69 commented Dec 28, 2024

Is your feature request related to a problem? Please describe.
Currently, when I subscribe to a topic more than once it requires only one unsubscribe call to actually stop receiving messages on given topic.
In a large app, that multiple modules may want to listen to some topics on demand, but stop subscription whenever that's not needed anymore.

Describe the solution you'd like
If I subscribe to given topic more than once, it should require exactly the same amount of unsubscribe calls to actually stop for listening to given topic.

Describe alternatives you've considered
Alternative library, if one exists.

Additional context
This is slightly related to #1951 as well, which is another pain point of mqtt.js. If I'd like to create such a wrapper described in the issue - I would bump into problem described here if I'd want to unsubscribe. I'd need to count subscriptions myself and basically wrap the whole library (overridding functions) to make it safe to use with any code.

@robertsLando
Copy link
Member

In my opinion such logic should not be handled inside mqtt itself as it's out of the scope of mqtt protocol. My approach when using mqttjs is to always create a wrapper around the client, in this way you can easily keep track of the subscriptions of each topic and implement the logic described above.

@dzek69
Copy link
Author

dzek69 commented Jan 28, 2025

If that's the case I think it should be stated in the README. It's perfectly fine to have a "raw" protocol client, but it must clear that there is a lot of real work to do before this can be used in an app without falling into pitfalls like that.

BTW: Is there any well-known client that wraps mqtt.js?

@robertsLando
Copy link
Member

It's perfectly fine to have a "raw" protocol client, but it must clear that there is a lot of real work to do before this can be used in an app without falling into pitfalls like that.

I'm not aware of other clients libraries that work different then this one does. Some allow to attach a subscribe callback but that's the only thing, the one related to subscribe/unsubscribe is really something that is out of the scope of the library, some may want exactly the opposite you are asking and introducing this behaviour would be a breaking change.

BTW: Is there any well-known client that wraps mqtt.js?

I'm not aware of any wrapper I wrote some on my personal projects but it's not a big deal to build one that fits your needs, it's quite easy

@dzek69
Copy link
Author

dzek69 commented Jan 28, 2025

It's quite easy if you know the protocol and the client library so well that you are actually aware of all the limitations. How many details like that are not obvious - I don't know.

@robertsLando
Copy link
Member

I use many protocol libraries out there (just to mention some Modbus, Z-Wave, Opcua, M-Bus, S7) and I always had to create a wrapper around all of them and MQTT is the easier one, try to see ZwaveClient one as an example to understand what I mean. I cannot ask z-wave driver author to implement all those features as its focus must be to support all protocol requests then users should develop on top of it.

@robertsLando
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants