MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

how to unsubscibe all topics

Open lujiashun1 opened this issue 1 year ago • 1 comments

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 avatar Aug 15 '24 01:08 lujiashun1

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?

SeppPenner avatar Aug 20 '24 15:08 SeppPenner

@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.

psp515 avatar Jan 01 '25 20:01 psp515

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

chkr1011 avatar Jan 04 '25 08:01 chkr1011