Skip to content

[BUG] mqtt-lib broker lib does not save retained message properties #77

@afshinpir

Description

@afshinpir

Environment:

  • mqtt-v5 version: 0.31.4
  • Rust version: 1.92.0
  • OS: Ubuntu 24.04.1 (on WSL)
  • MQTT Broker (if applicable):

Description:
Retained message does not save published message properties in the broker. I have tested this with response_topic and correlation_data properties. This is the mqtt-lib broker issue and it does not happens if I use another broker (e.g. nanomq).

To Reproduce:
Run mqtt-lib broker. Publish a retained message with response_topic and correlation_data to a topic. Then subscribe to that topic after message is published.

Run broker from command line with following command:

mqttv5 broker --allow-anonymous --storage-backend memory

and the run following code:

use mqtt5::{MqttClient, QoS, PublishOptions, PublishProperties};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client1 = MqttClient::new("my-device-001");
    client1.connect("mqtt://localhost:1883").await?;

    let client2 = MqttClient::new("my-device-002");
    client2.connect("mqtt://localhost:1883").await?;

    let options = PublishOptions {
        qos: QoS::ExactlyOnce,
        retain: true,
        properties: PublishProperties {
            response_topic: Some(String::from("sensors/response")),
            ..Default::default()
        },
        ..Default::default()
    };

    // Publish a message
    client1.publish_with_options("sensors/request", b"25C", options).await?;

    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;

    // Subscribe with callback
    client2.subscribe("sensors/request", |msg| {
        println!("topic: {} payload: {}", msg.topic, String::from_utf8_lossy(&msg.payload));
        println!("response_topic: {}", msg.properties.response_topic.map_or(String::from("response topic is None!"), |v| v));
    }).await?;

    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
    Ok(())
}

Expected behavior:
The broker need to send response_topic and correlation_data as well for the retained message. In above code, I need to see response_topic: sensors/response in the output.

Actual behavior:
You will receive the message body, but response_topic and correlation_data are set to None. I see response_topic: response topic is None! in the output.

Additional context:

  • Does this happen consistently or intermittently? Consistently
  • Have you tested with different MQTT brokers? Yes. I have tested code with nanomq broker and I saw expected behavior there.
  • Are you using any specific MQTT v5.0 features? Yes

For AI Agents:

  • Verify reproduction steps are complete
  • Check if this matches any known issues
  • Validate environment information is complete
  • Ensure error messages are included

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions