Building an MQTT Client in MQL5 with Test-Driven Development
Summary
This article continues a project to build a native MQTT client in MQL5, focusing on constructing a valid CONNECT packet. The earlier attempt sent a fixed header that the local Mosquitto broker rejected because required variable-header fields were missing. The author first writes a test comparing the generated packet with a reference byte array, then organizes protocol constants and shared structures in header files and develops a packet-building class using object-oriented design.
The article reports that the completed packet passed its comparison test and that the broker accepted the connection without the earlier protocol error. This illustrates how unit tests can describe packet requirements before implementation and catch malformed protocol data before an integration check. The scope is limited to connection setup: server acknowledgments, message exchange, and broader operational behavior are left for later work. MQTT is presented as a possible messaging layer for trading applications, but this installment does not evaluate a trading use case, latency, reliability under load, or strategy performance.
Key ideas
- The MQTT CONNECT packet needs a variable header as well as a fixed header to meet protocol requirements.
- A test can specify the expected packet bytes before the packet builder is implemented.
- Separating protocol constants from evolving structures and functions helps organize a client library.
- A local broker provides an integration check after the packet passes its unit test.
- The article demonstrates connection setup only; it does not yet cover acknowledgments or message exchange.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.