rumqtt icon indicating copy to clipboard operation
rumqtt copied to clipboard

refactor Mqttoptions

Open swanandx opened this issue 1 year ago • 1 comments

This PR aims to improve usability of Mqttoptions and fixes #434 and #270 . some of the changes are:

  • port isn't required argument to new():

    • It can be passed in broker address, e.g. localhost:1337, if not specified, defaults ports will be used as per MQTT specifications.
    • set_port() can be used to set/override the port
  • Passing partial url as address works:

    • localhost, localhost:1883, ws://localhost, mqtts://localhost:1884, wss://example.org/mqtt etc. are all valid addresses
    • passing invalid addresses like swanx://localhost, localhost:999999, localhost:x2 return errors regarding scheme / port.
  • Same behavior of MqttOptions for both tcp and webscokets :)

Issues / queries:

  • instead of returning Error from MqttOptions::new(), shall we panic if provided options are wrong? why?: because we do same for other fns like set_keep_alive / set_inflight. why not?: rumqttc is used as library, returning error so users can handle em as they want sound better than panics.

  • we aren't performing any host validations, i.e. MqttOptions::new("client_id", "[@-1") won't return any error. Rather it would try to connect to the host and fail with lookup error: Io(Custom { kind: Uncategorized, error: "failed to lookup address information: Name or service not known" }). Shall we do strict hostname checking? why?: we would catch some of the invalid address early ( like empty hostname, invalid IPv6 etc.) why not?: It is hard to validate! using other dependency like url::Host seems good but it doesn't support no_std which we would like to have :(

Please comment your thoughts, I will mark the PR as ready once above/any other queries are resolved :)

Type of change

  • Breaking change

Checklist:

  • [x] Formatted with cargo fmt
  • [ ] Make an entry to CHANGELOG.md if it's relevant to the users of the library. If it's not relevant mention why. TODO: once we have finalized it!

swanandx avatar Jan 29 '24 11:01 swanandx

What's currently missing here to be merged? I'm hitting problem with parsing ws:// URLs right now too.

elrafoon avatar Sep 11 '25 13:09 elrafoon