MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

WithNoLocal(true) fails to suppress receipt of message

Open PeterWone opened this issue 4 years ago • 6 comments

Describe the bug

WithNoLocal(true) fails to suppress receipt of message a message published by the same client.

Which project is your bug related to?

  • ManagedClient

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of MQTTnet 3.0.15
  2. Run this code against current Mosquitto, either a local instance unauthenticated or running on another computer configured using the default password file remembering to uncomment the WithCredentials line.
using System;
using System.Threading;
using MQTTnet;
using MQTTnet.Client.Options;
using MQTTnet.Client.Receiving;
using MQTTnet.Extensions.ManagedClient;
using MQTTnet.Formatter;

namespace MockApp
{
  class Program
  {
    static string ClientId = $"MockApp2_{DateTime.Now.Ticks}";
    static void Main(string[] args)
    {
      // Setup and start a managed MQTT client.
      var options = new ManagedMqttClientOptionsBuilder()
      .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
      .WithClientOptions(new MqttClientOptionsBuilder()
          .WithProtocolVersion(MqttProtocolVersion.V500)
          .WithClientId(ClientId)
          .WithTcpServer("localhost")
          // .WithCredentials("roger", "password")
          .Build())
      .Build();

      var mqttClient = new MqttFactory().CreateManagedMqttClient();
      mqttClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(mre =>
      {
        var payload = mre.ApplicationMessage.ConvertPayloadToString();
        Console.WriteLine($"RECV {payload}");
      });
      mqttClient.StartAsync(options).Wait();

      var arbitraryTopicName = "wibble";
      mqttClient.SubscribeAsync(new MqttTopicFilterBuilder()
      .WithTopic(arbitraryTopicName)
      .WithNoLocal(true)
      .Build());

      Thread.Sleep(300);
     
      mqttClient.PublishAsync(arbitraryTopicName, "This should not be seen by ApplicationMessageReceivedHandler");

      Console.WriteLine(ClientId);
      Console.ReadKey();
    }
  }
}

  1. Local message is received.

Expected behavior

.WithNoLocal(true) should suppress receipt of the message.

Screenshots

image

PeterWone avatar Jun 29 '21 08:06 PeterWone

I faced the same problem

AgPeHaJIuH1 avatar Aug 14 '21 20:08 AgPeHaJIuH1

This issue still exists in v3.0.16.

tgrtb avatar Oct 27 '21 09:10 tgrtb

This will be fixed in the next version 3.1.0. It supports more MQTTv5 features.

chkr1011 avatar Oct 27 '21 14:10 chkr1011

I face the same problem with version 3.1.2 when sending many messages. Because of how my app works i have to publish about 1000 messages when establishing the connection. With a test program I was able to reproduce this problem at about 250 messages.

Before I switched to the ManagedClient I was using the normal Client and it didn't have this issue.

Weirdly the problem doesn't persist when setting NoLocal=true in a SubscriptionInterceptor.

Luki936 avatar Feb 09 '22 14:02 Luki936

Hi, we have same issue with the ManagedClient and version 3.1.2 even with single messages.

ChilliPommes avatar Feb 09 '22 14:02 ChilliPommes

@ChilliPommes @Luki936 The Managed Client does not store this value internally so it can't work with the managed client right now. It only stores the topic and the QoS. I will have a look after 4.0 is released. But anyone is welcome to create a PR which stores the entire topic filter in the ManagedClient instead of the topic and QoS only.

chkr1011 avatar Feb 11 '22 21:02 chkr1011

The latest verion 4.0.2 now stores the entire topic filter so that "NoLocal" is also properly sent to the broker.

chkr1011 avatar Sep 25 '22 08:09 chkr1011