Skip to content

Conversation

@fridayy
Copy link

@fridayy fridayy commented Nov 3, 2025

Hello!
While playing with AtomVM and this library I made the following observations:

  • Currently username/password authentication does not work as the values where never passed into esp_mqtt_client_config_t and never made it into the port
  • Passing a client-id has no effect (always auto-generated as the value never made it so the port)
  • trusted_cert currently serves no purpose (never passed into the port)

This PR should fix the first two points (tested with self-hosted mosquitto) and removes trusted_cert for now.

Thank you for your great work on AtomVM - love it. ❤️

%% Start the MQTT client.
%%
Config = #{
url => "mqtt://mqtt.eclipseprojects.io",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with a working alternative

init(Config) ->
try
Self = self(),
Port = erlang:open_port({spawn, "atomvm_mqtt_client"}, [
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

username, password, client_id never made it into the port as they were missing from the opts here.

@fridayy fridayy force-pushed the fix/consider-username-and-password branch from ca2f793 to b68dd52 Compare November 3, 2025 14:44
@UncleGrumpy
Copy link
Contributor

Thank you for raising the issue. This has been broken for a while, but I assumed it was my broker (I was testing on a local broker) that did not the way the authentication was being sent… but as you point out it wasn’t!

As a temporary workaround you can add the username and password to the broker URL:

mqtts://USER:[email protected]

Now that I know this is not just a personal problem, I will work on a proper fix. Thanks again for opening the issue, and please don’t hesitate to open issues for any other problems you encounter, or suggestions for improvements in the AtonVM ecosystem.

@fridayy
Copy link
Author

fridayy commented Nov 3, 2025

Thank you for raising the issue. This has been broken for a while, but I assumed it was my broker (I was testing on a local broker) that did not the way the authentication was being sent… but as you point out it wasn’t!

As a temporary workaround you can add the username and password to the broker URL:

mqtts://USER:[email protected]

Now that I know this is not just a personal problem, I will work on a proper fix. Thanks again for opening the issue, and please don’t hesitate to open issues for any other problems you encounter, or suggestions for improvements in the AtonVM ecosystem.

You're welcome @UncleGrumpy - happy to help.

Copy link
Contributor

@UncleGrumpy UncleGrumpy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! I don't have any maintainer privileges in this repo, but I wanted to leave some comments and suggestions to help speed up the review process when a maintainer gets a chance to review it.


// NB. Caller assumes ownership of returned string
static char *maybe_get_string(term kv, AtomString key, GlobalContext *global)
static char* maybe_get_string_or_default(term kv, AtomString key, char* default_value, GlobalContext *global)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep consistent with the AtomVM C Coding Style Guide and put the pointer preceding the variable in char *default_value.

UNUSED(port);
char *username_str = maybe_get_string(opts, username_atom, global);
char *password_str = maybe_get_string(opts, password_atom, global);
// char *cert_str = maybe_get_string(opts, cert_atom, global);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not remove this commented code, left by the original author as a reminder, or at least change it to a TODO: comment to add certificate based authentication.

.uri = url_str,
.username = username_str,
.password = password_str
.client_id = client_id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

.credentials.client_id = client_id
.credentials = {
.username = username_str,
.client_id = client_id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.
.client_id = client_id, > .client_id = client_id_str,

char *username_str = maybe_get_string(opts, username_atom, global);
char *password_str = maybe_get_string(opts, password_atom, global);
// char *cert_str = maybe_get_string(opts, cert_atom, global);
char *client_id = maybe_get_string_or_default(opts, client_id_atom, get_default_client_id(), global);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep the naming scheme consistent and use client_id_str here, which would require updating lines 802 and 811 below.

url => "mqtt://broker.hivemq.com",
% username => "some-user", % optional
% password => "some-password", % optional)
% client_id => "atomvm-client", % optional (generated if missing)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default name is atomvm-<DEVICE-MAC-ADDRESS>, so every device will have a unique default name. That may not be the best way of presenting it, but that gives you the idea.

@fridayy
Copy link
Author

fridayy commented Nov 4, 2025

Thank you for the contribution! I don't have any maintainer privileges in this repo, but I wanted to leave some comments and suggestions to help speed up the review process when a maintainer gets a chance to review it.

Thanks for the feedback, very much appreciated. I will implement the changes sometime after work. Have a great day @UncleGrumpy

@UncleGrumpy
Copy link
Contributor

This is going to sound a little nit-picky, but we like to maintain a clean git history for all of the AtomVM repos. I would suggest squashing the two commits (and remove the reference to requested PR changes - nearly every PR gets some small change requested).

Optionally, you can separate out the code changes to made in the second commit (addressing PR comments) and flatten those into the first commit, and then have all the typo fixes separated into the second commit and amend the message accordingly. The second is often what would be preferred for the AtomVM repo, but overall this is a small set of changes so either is fine here.

@fridayy
Copy link
Author

fridayy commented Nov 5, 2025

This is going to sound a little nit-picky, but we like to maintain a clean git history for all of the AtomVM repos. I would suggest squashing the two commits (and remove the reference to requested PR changes - nearly every PR gets some small change requested).

Optionally, you can separate out the code changes to made in the second commit (addressing PR comments) and flatten those into the first commit, and then have all the typo fixes separated into the second commit and amend the message accordingly. The second is often what would be preferred for the AtomVM repo, but overall this is a small set of changes so either is fine here.

Not nit-picky at all - I'll squash them ofc. 👍
Thanks again for your review and your time @UncleGrumpy

@fridayy fridayy force-pushed the fix/consider-username-and-password branch 2 times, most recently from e56f1b8 to 9eb4fce Compare November 5, 2025 18:06
…ort custom mqtt client ids; fix documentation typos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants