eclipse / paho.mqtt.rust

paho.mqtt.rust
Other
527 stars 102 forks source link

mqtt_version of CreateOptionsBuilder seems to be ignored #140

Closed koepalex closed 2 years ago

koepalex commented 2 years ago

In my first approach of using the paho mqtt crate I added my settings to CreateOptions like

let create_opts = mqtt::CreateOptionsBuilder::new()
        .server_uri(host)
        .mqtt_version(mqtt::MQTT_VERSION_5)
        .client_id("my-publisher-id")
        .finalize();

let cli = mqtt::AsyncClient::new(create_opts).unwrap_or_else(|err| {
        error!("Error creating the client: {}", err);
        process::exit(1);
    });

match cli.connect(None).await {
        Ok(res) => info!("Connected with Response: {}", res.reason_code()),
        Err(err) => panic!("Error connecting to MQTT broker:: {}", err),
    }

let msg = mqtt::Message::new(topic, data.dump().into_bytes(), qos);
let delivery_token = cli.publish(msg).wait();

by using cli.connect(None) I expect to use the settings of CreateOptions but at least the mqtt_version is ignored and mqtt_version 0 is used.

fpagliughi commented 2 years ago

Sorry for the slow response...

Yeah, I agree this is confusing. I simply exposed the behavior of the underlying C library, and I'm not particularly happy about having to specify the version twice, either.

I think the C lib wants to know if you might use a v5 connection when you create the client so it can create some persistence information differently. But it obviously still lets you create a v3 connection... which you found out by accident :-)

We could take the hint from the create option to be the default when connecting. That would certainly be better than the way it works now.

But even better than that maybe either:

So, either way, chose the version in the connect options, but decide whether or not to even keep the option when creating the client.

fpagliughi commented 2 years ago

OK. The connection will default to the version of MQTT specified in the client's create options. Basically, now you can chose to connect with an older version of the protocol than the one specified when creating the client, but you can not connect with a newer one...

Meaning:

If you create the client as v5:

If you create the client as v3.x: