Search code examples
mqttmosquittomqttnet

MQTT5 User Properties with Mosquitto Bridge


I am running a local Mosquitto (MQTT) Broker that connects to a remote Mosquitto Broker using the build in MQTT Bridge functionality of Mosquitto. My mosquitto.conf looks like this:

# =================================================================
# Listeners
# =================================================================
listener 1883

# =================================================================
# Security
# =================================================================
allow_anonymous true

# =================================================================
# Bridges
# =================================================================
connection myConnectionName
address <<Remote Broker IP>>:1883
remote_username <<Remote Broker Username>>
remote_password <<Remote Broker Password>>
topic mytopic/# out 1 "" B2/
bridge_protocol_version mqttv50

cleansession false
bridge_attempt_unsubscribe true

upgrade_outgoing_qos true

max_queued_messages 5000

For testing I run a MqttPublisher using a C# console application which uses the MQTTnet library (version 3) and a MqttSubsbriber (also C# console application with MqttNet).

Now I want the Publisher to publish MQTT messages with User Properties (introduced by MQTT 5). I build the message like this:

using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Options;

class Program
{
    static void Main()
    {
        // Create a new MQTT client instance
        var factory = new MqttFactory();
        var mqttClient = factory.CreateMqttClient();

        // Setup the options for the MQTT client
        var options = new MqttClientOptionsBuilder()
            .WithClientId("MqttPublisher")
            .WithTcpServer("localhost", 1883)
            .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500)
            .WithCleanSession()
            .Build();

        mqttClient.ConnectAsync(options).Wait();
        var i = 0;
        while (true)
        {
            Console.WriteLine("Client connected: " + mqttClient.IsConnected);
            var message = new MqttApplicationMessageBuilder()
                .WithTopic("mytopic/test")
                .WithUserProperty("UPTest","Hi UP")
                .WithPayload("Hello World: " + i)
                .Build();

            mqttClient.PublishAsync(message).Wait();

            Console.WriteLine("Published message with payload: " + System.Text.Encoding.UTF8.GetString(message.Payload));
            i++;
            System.Threading.Thread.Sleep(1000);
        }

        mqttClient.DisconnectAsync().Wait();
    }
}

With the subscriber (also with WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500) if I subscribe to the topic I get all the messages and I can read the MQTTnet.MqttApplicationMessage like shown in the following screenshot: enter image description here

The messages are also published to the remote MQTT Broker due to the MQTT Bride configured. However, if I subscribe to the remote Broker with my MqttSubscriber, I am not getting the User Properties anymore: enter image description here

Is there any way to configure the Mosquitto Bridge that also the user properties are send? I cant find a way and any help and comments are appreciated.

Thanks Joshua


Solution

  • Using mosqutto 2.0.15 I have verified that MQTTv5 message properties are passed over the bridge.

    Broker one.conf

    listener 1883
    allow_anonymous true
    

    Broker two.conf

    listener 1884
    allow_anonymous true
    
    connection one
    address 127.0.0.1:1883
    topic foo/# both 0
    bridge_protocol_version mqttv50
    

    Subscriber connected to Broker two

    $ mosquitto_sub  -t 'foo/#' -V mqttv5 -p 1884 -F "%t %P %p"
    foo/bar foo:bar ben
    

    Publisher connected to Broker one

    $ mosquitto_pub -D PUBLISH user-property foo bar -t 'foo/bar' -m ben -p 1883
    

    As you can see the the %P in the output format for the subscriber is outputting the user property foo with a value of bar when subscribed over the bridge.