xamarin / mqtt

Minimalist and intuitive MQTT broker and client written entirely in C#
http://xamarin.github.io/mqtt
MIT License
302 stars 72 forks source link

Why is message received over and over again? #160

Closed ddobric closed 1 year ago

ddobric commented 6 years ago

By following you example, I have implemented subscriber as shown below:

this.m_MqttClient.MessageStream .Where(msg => msg.Topic == this.m_RcvTopic) .Subscribe(async msg => {
await msgReceiver?.Invoke(msg.Payload);

The message is sent by using this code: await this.m_MqttClient.PublishAsync(new MqttApplicationMessage(this.m_SendTopic, message), MqttQualityOfService.ExactlyOnce);

The message is correctly sent and received, but receiving of the same message does not stop. The message is received over and over again. What is the correct way to reliably complete receiving of the message?

Thanks

kzu commented 6 years ago

Are SendTopic and RcvTopic the same? Have you subscribed the client with that topic before subscribing to the observable? What is the server being used? Is it System.Mqtt.Server too or a third party?

Thanks!

ddobric commented 6 years ago

It looks as this would happen if publisher and receiver use same ClientID. This is for sure not a production case (it happened during invalid unit test configuration), but I'm not sure if it is correct.

kzu commented 6 years ago

The second connection using the same client ID should cause the previous one to be forcefully disconnected, according to the spec. I believe that is currently the case, right @mauroa?

ddobric commented 6 years ago

It does not behave this way. Second does not cause the first one to be disconnected. First one is continuously receiving the message. To me this does not look correct.

mauroa commented 6 years ago

Can you please try changing the Quality of Service to a lower value? (either 0 or 1). You are using QoS 2 in your samples (MqttQualityOfService.ExactlyOnce). It’s just to discard an issue with it. Thanks.

kzu commented 6 years ago

Are you using this MQTT implementation for both client and server? If so, can you provide the full failing unit test so we can repro?

Thanks!

ddobric commented 6 years ago
  1. I'm using MqttQualityOfService.ExactlyOnce
  2. Use same library for client and server in the same process inside of unit tests and also in different processes.

As I mentioned, the reason was using of same ClientID for publisher and subscriber.

daspuru commented 6 years ago

Hi, I am testing the server and the client (this implementation), and have a similar issue. When I suscribe to a topic, and then send only one message to that topic, the message is received and received infinitely at intervals for about 5 seconds. It is the same client, in the same app. First I suscribe to the topic and then send the message.

If I send the message from another client, the message just arrives once, but sending it from the same client makes this weird behavior...

Thanks a lot!

daspuru commented 6 years ago

Hi all, I fixed the issue staring the server with the same configuration as the client (QoS 2), as I was not introducing any parameter in the MqttServer.Create() method.

Now it seems to work great! :)

ddobric commented 6 years ago

Good job 👍 Which nuget version contains the fix?

romannovotny commented 5 years ago

I think it still not working correctly on version I get from nuget in visual studio. I still get one topic after a while. And dont get nothing on the beginning. And when I use Guid as ClientId it is not working at all. So I came back to M2Mqtt