# mqtt
[TOC]
debug client
- [mqttx](https://mqttx.app/)
## spec
什麼功能重要
- [wiki Comparison of MQTT implementations](https://en.wikipedia.org/wiki/Comparison_of_MQTT_implementations)
- 參考就好,不少資料過時
和mqtt無關的功能不需考慮。
- mqtt v3.1, 3.1.1, 5.0
- 肯定是必要的
- SSL/TLS 加密
- 必要
- mqtt-SN (v1.2)
- 有很好,原因在於本來 MQTT 是沒有UDP 相關的protocol 的。
- lora, ble, zigbee...這種 。mqtt-sn 確實 往下 拓展了應,cost down。
- MQTT over WebSockets (WS)
- websocket 最大的好處是讓本來只有TCP 的MQTT,可以直接擴展到 web frontend。算是不錯的 feature。
- websocket 現在的處境有點尷尬。因為 QUIC/http 要出來了。
- MQTT over WebSockets with TLS (WSS)
- 同上
- MQTT over QUIC
- webpage 還是最大的利基
- QUIC 底下是 UDP,比 TCP 輕很多,所以 websocket 可能被完全取代。
- 另外 QUIC 有類似TCP的保證傳到功能。
- 一般mqtt 可能比 mqtt over QUIC 輕量。(要測)
- 不同於 websocket 已經訂入 spec , quic 還在draft 階段。
- Clustering
- 對大 server
### summary
- mqtt-SN 要有
- MQTT over QUIC 還沒進入spec,現在要求這個不太合理
- mqtt over websocket 還是有比較好。
- web dashboard 有是很好,但是log 一般就夠了(且loading 更少)。
- 我對 Clustering 還沒有興趣
## broker
broker 幾乎不需要考慮自己需要開發的情況。
- mosquitto (broker)
- MQTT 3.1, 3.1.1, and 5.0
- MQTT-WS
- [FlashMQ](https://github.com/halfgaar/FlashMQ)
- C++ lang
- MQTT 3.1, 3.1.1 and 5.0
- mqtt-ws
- no mqtt-sn
- [vernemq](https://github.com/vernemq/vernemq)
- MQTT 3.1, 3.1.1 and 5.0
- no MQTT-SN
- cluster
- **emqx**
- erlang
- MQTT 3.1, 3.1.1 and 5.0
- [EMQX vs Mosquitto | 2023 MQTT Broker 对比](https://zhuanlan.zhihu.com/p/623630147)
- mqtt-SN
- MQTT-WS
- mqtt over quic
- cluster
- 幾個 benchmark 都不算差。
- default 就有 dashboard
- [nanomq](https://github.com/nanomq/nanomq)
- c lang
- by actor model
- [EMQX vs NanoMQ | 2023 MQTT Broker 对比](https://www.emqx.com/zh/blog/emqx-vs-nanomq-2023-mqtt-broker-comparison)
- MQTT 3.1, 3.1.1, and 5.0 (部份不完整)
- mqtt over quic
- [Mosquitto vs NanoMQ | 2023 MQTT Broker 对比](https://www.emqx.com/zh/blog/mosquitto-vs-nanomq-2023-mqtt-broker-comparison)
- [rabbitmq](https://github.com/rabbitmq/rabbitmq-server)
- multi protocol, with mqtt
- MQTT 3.1, 3.1.1, and 5.0
- Coolpy7
- [MQTTnet](https://github.com/dotnet/MQTTnet) (broker)
- microsoft 官方
- **JORAM**
- java
- MQTT 3.1, 3.1.1, and 5.0
- mqtt-sn
- mqtt-ws
- cluster
- [aedes](https://github.com/moscajs/aedes) (from mosca)
- js
- MQTT 3.1 and 3.1.1, partial 5.0
- [activemq](https://activemq.apache.org/components/artemis/documentation/latest/mqtt.html)
- java
- MQTT 3.1, 3.1.1, and 5.0
- 主要是 for JMS
- mqtt-ws
- [async_mqtt](https://github.com/redboltz/async_mqtt)
- from mqtt_cpp
- boost asio 幾乎保證 high performance,沒起來可惜
### benchmark
- [A Collection of 20+ MQTT Broker Performance Benchmarks (2020–2023)](https://www.altoroslabs.com/blog/a-collection-of-mqtt-broker-performance-benchmarks-2020-2023/)
- flash mq 肯定是最快的
- [emqx blog-Scalability, Performance, and Reliability](https://github.com/emqx/blog/blob/main/en/202304/a-comprehensive-comparison-of-open-source-mqtt-brokers-in-2023.md)
- throughput: emqx> nanomq> mosquitto
- [flashmq vs nanomq vs emqx vs mosquitto](https://youtu.be/ejS0j1HjVdk?feature=shared&t=441)
- 這和預期的比較相符,C 語言+ actor model 的 nanomq 比 erlang 的 emqx through put 低就很怪。
- [ mqtt-broker-benchmark](https://github.com/lkskrt/mqtt-broker-benchmark)
- mosca>emqx>mosquitto>>rabbitmq
### summary
spec的要求
- ultra low cost/power, memory, edge device.
- mosquitto
- aware no mqtt-sn
- desktop/server
- mosquitto
- emqx
- 有 dashboard
- 多耗一點資源
- 如果不需要 mqtt-sn 了話,有更好的選擇
- falshMQ
- cluster server
- emqx
## client
- mosquitto (client)
- paho (client)
- [NanoSDK](https://github.com/emqx/NanoSDK) (nanomq client)
- [paho.mqtt.c](https://github.com/eclipse/paho.mqtt.c)
- > paho 只有 client
- MQTT 3.1, 3.1.1, and 5.0
- MQTT-SN
- MQTT-WS
- [MQTTnet](https://github.com/dotnet/MQTTnet) (client)
- microsoft 官方
其它
- [Benchmarking popular MQTT + JSON implementations](https://flespi.com/blog/benchmarking-popular-mqtt-json-implementations)
- [MQTT-Client-Examples](https://github.com/emqx/MQTT-Client-Examples)
- emqx 出的
### summary
client 這裡spec 的要求不太一樣,quic, websocket 對於非js 沒有什麼用。
mqtt over quic,要能讓非JS 的 client 也能用,恐怕有的等,而且還沒測有沒有更好,所以不列入考慮。
- mcu/ultra lowcost
- mosquitto
- 雖然只有 standard mqtt
- paho 好像有 embed c,可以配 mqtt-sn
- 一般
- paho (cpp) 應用最廣的
- paho 還有很多語言。
- web (js only)
- ?
## mosqitto
還是標準
而且是最簡單操作的,所以可以當作一套工具(驗證, 開發)。
### mosquitto config
- install
- [ Day25 - AWS EC2 安裝 Mosquitto ](https://ithelp.ithome.com.tw/articles/10226629)
- windows
- [| ESP32 教學 | Mosquitto conf 設定與 MQTT 測試](https://jimirobot.tw/esp32-mosquitto-conf-mqtt-tutorial/)
- windows 的版本和其他不同 包括要關掉防火牆, 設定檔不同
- `mosquitto.exe -v -c mosquitto.conf`
- configure
- `sudo nano /etc/mosquitto/mosquitto.conf`
```ini
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
# user add
allow_anonymous true
listener 1883 0.0.0.0
sys_interval seconds
log_dest topic
log_timestamp_format %Y-%m-%dT%H:%M:%S
#log_type error
#log_type warning
log_type all
```
#### log
- log
- [Understanding and Configuring Logging – Mosquitto Broker Configuration](http://www.steves-internet-guide.com/mosquitto-logging/)
- default log 在 `sudo cat /var/log/mosquitto/mosquitto.log`
-
```
1710830275: New connection from 192.168.29.230:40922 on port 1883.
1710830275: New connection from 192.168.29.230:40923 on port 1883.
1710830275: New client connected from 192.168.29.230:40923 as testClientBB (p2, c0, k60).
1710830275: New client connected from 192.168.29.230:40922 as testClientAA (p2, c0, k60).
1710830279: New connection from 192.168.29.130:4937 on port 1883.
1710830279: New client connected from 192.168.29.130:4937 as mqttx_0f75ecb4 (p5, c1, k60).
1710830279: Client mqttx_0f75ecb4 disconnected.
1710830279: New connection from 192.168.29.130:4938 on port 1883.
1710830279: New client connected from 192.168.29.130:4938 as mqttx_3e7eb544 (p5, c1, k60).
1710830280: New connection from 192.168.29.130:4939 on port 1883.
1710830280: New client connected from 192.168.29.130:4939 as mqttx_0f75ecb4 (p5, c1, k60).
```
- `sudo cat /var/log/mosquitto/mosquitto.log | grep mqttx_92d7d793`
- [mosquitto.conf man page](https://mosquitto.org/man/mosquitto-conf-5.html)
- 官方手冊
- 換掉unix 時間: `log_timestamp_format %Y-%m-%dT%H:%M:%S`
- 要得到millisecond ,要用 syslog。 [ log_timestamp_format & Millisecond #1389 ](https://github.com/eclipse/mosquitto/issues/1389)
### operation
- operation
- `sudo systemctl restart mosquitto`
- message
- `mosquitto_sub -t "pingpong/#"`
## version
- [[深入淺出MQTT]: v3.1.1與v5 的差異](https://xiang753017.gitbook.io/zixiang-blog/broker/shen-ru-qian-chu-mqttv3.1.1-yu-v5-de-cha-yi#mqtt-v3.1.1-v.s-mqtt-v5-neng-li-bi-jiao)
- [wiki Version 5.0](https://en.wikipedia.org/wiki/MQTT)
- mqtt over QUIC
- [MQTT Specifications](https://mqtt.org/mqtt-specification/)
-