Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use built-in async for mqtt #24786

Merged
merged 14 commits into from
Nov 19, 2024
Merged

fix: Use built-in async for mqtt #24786

merged 14 commits into from
Nov 19, 2024

Conversation

Nerivec
Copy link
Collaborator

@Nerivec Nerivec commented Nov 15, 2024

  • Use built-in async version of mqtt functions, should fix some promise awaiting weirdness (also allows a nice clean up of tests...)
  • Add logging on publish error
  • Fix typing
  • Add more coverage
  • Add handler for disconnect event (when using version: 5)
  • Set maximum packet size in CONNECT packet (from settings)

Also tested manually on mosquitto and nanomq on top of jest tests...

@Koenkk Some notes:

  • v5 features need a closer look from someone with a better knowledge of the MQTT spec (could be in follow-up PR though)
    • DISCONNECT seems to not really be respected by brokers (can't get it to trigger from nanomq when exceeding maximum packet size, it just drops the packet)?
    • might need more handling for disconnect event... might not reconnect?

Ref: https://www.emqx.com/en/blog/mqtt5-new-features-reason-code-and-ack

Re #24625

CC: @corporategoth @mundschenk-at

Docs Koenkk/zigbee2mqtt.io#3228

@corporategoth
Copy link

I should note that the default max packet size on EMQX is 1mb. So probably want to lower the default size we send to 1mb.
Also, might not be a bad idea to make that a configurable option in the MQTT settings.

@mundschenk-at
Copy link
Contributor

I think the side effects would mainly be memory-related, i.e. buffer size both on the broker and client sides.

@Nerivec
Copy link
Collaborator Author

Nerivec commented Nov 17, 2024

With the configurable setting, and the logging on disconnect with reason, it should be easy enough to track down and fix improper configurations. Allowing to leave the default at 1MB (which already amounts to roughly 3.5KB per device * 300 devices for /bridge/devices... should cover most networks with ease).

FYI, broker defaults seem to be:

  • mosquitto: no limit (i.e. 256MB)
  • emqx: 1MB
  • nanomq: 2MB


this.client.on('message', this.onMessage);

await this.onConnect();
Copy link
Owner

Choose a reason for hiding this comment

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

This method logs logger.info('Connected to MQTT server'); while it may not be the case yet, I'm also not sure about the subscribe before it gets connected?

@Nerivec Nerivec marked this pull request as ready for review November 19, 2024 20:21
@Koenkk Koenkk merged commit eab92f0 into Koenkk:feat/2.0.0 Nov 19, 2024
11 checks passed
@Nerivec Nerivec deleted the mqtt-async branch November 19, 2024 22:02
Koenkk pushed a commit that referenced this pull request Dec 1, 2024
* fix: Use async for mqtt.

* Update mocks, fix expects

* Prettier.

* Use appropriate error for publish

* Set max listeners immediately after connect

* Add mqtt v5+ `disconnect` event handler

* Set maximum packet size in `CONNECT` packet

* Fix tests.

* Add setting for maximum packet size.

* Add `reasonString` to disconnect logs

* Fix tests

* Fix

* Prettier
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.

4 participants