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

how to unsubscibe all topics #2063

Closed
lujiashun1 opened this issue Aug 15, 2024 · 3 comments
Closed

how to unsubscibe all topics #2063

lujiashun1 opened this issue Aug 15, 2024 · 3 comments
Labels
question It is a question regarding the project

Comments

@lujiashun1
Copy link

lujiashun1 commented Aug 15, 2024

MqttTest.zip
github
`var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()

            .Build();
        //MqttClientUnsubscribeOptions mqttClientUnsubscribeOptions = new MqttClientUnsubscribeOptions();
        //mqttClientUnsubscribeOptions.TopicFilters = mqttClient.Options.
        //await mqttClient.UnsubscribeAsync(mqttClientUnsubscribeOptions);
        mqttSubscribeOptions.TopicFilters = mqttTopicFilters;


        var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);`

when I click Subscribe several times,I received several identical messages at the same time, Can't it remove duplicate topics,or how to unsbuscribe all topics?

@lujiashun1 lujiashun1 added the question It is a question regarding the project label Aug 15, 2024
@SeppPenner
Copy link
Collaborator

You can subsribe to the same topic multiple times if you want, but you shouldn't. And you can unsubscribe using UnsubscribeAsync, so what's the point?

@psp515
Copy link

psp515 commented Jan 1, 2025

@lujiashun1 As far as I understand your problem, you want to avoid duplicate subscribtions.
So what can you do is to create some hash map and store there what topics have been subscibed already and before making new sub you just will check if you haven't already subscribed.

PS. Use lock statement that no parallel clicks subscibe to one topic twice.

@chkr1011
Copy link
Collaborator

chkr1011 commented Jan 4, 2025

The MQTT protocol requires that the user keeps track of subscriptions and make proper calls and checks to keep them clean.

@chkr1011 chkr1011 closed this as completed Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question It is a question regarding the project
Projects
None yet
Development

No branches or pull requests

4 participants