Get started
Follow this chapter to compile the synchronous sample client and verify broker connectivity.
Before you begin
Ensure you have:
TrustCore SDK U6 release or later (NanoMQTT is built in by default). To rebuild only NanoMQTT, run
./build.sh nanomqttfromprojects/mqtt_client/.A POSIX shell and a C11‑compatible tool‑chain.
Access to a public or private MQTT broker. We have taken the example of https://test.mosquitto.org.
Community edition: Build the sample client (with SSL)
cmake -DBUILD_SAMPLES=ON <options> -B build -S . cmake --build build
This creates the mqtt_client in samples/bin/mqtt_client.
CMake flags
CMake flag | Purpose |
|---|---|
-DENABLE_MQTT_STREAMING=ON | Enables MQTT streaming mode |
-DENABLE_MQTT_UNITTEST=ON | Enables MQTT client unit tests |
-DENABLE_MQTT_TEST=ON | Enables MQTT client functional tests |
Standard Edition: Build the sample client (with SSL)
cd mocn-mss ./scripts/nanomqtt/mqtt_client/build_mqtt_client.sh --ssl
The script creates the mqtt_client_sample in mocn-mss/bin/.
Quick publish/subscribe demo
Export a few helper variables:
export MQTT_SERVER=test.mosquitto.org # any MQTT‑5 capable broker export MQTT_PORT=1883 # TCP export MQTT_SSL_PORT=8883
Set the library path
Note
Note: For ARM builds, use the aarch64 instead of linux-x86_64.
export LD_LIBRARY_PATH=lib/:crypto_lib/linux-x86_64/:$LD_LIBRARY_PATH
Start a subscriber (plain TCP)
./samples/bin/mqtt_client \
--mqtt_servername ${MQTT_SERVER:-test.mosquitto.org} \
--mqtt_port ${MQTT_PORT:-1883} \
--mqtt_sub_topic demo/topic \
--mqtt_clean_startGet the CA certificate
Get the CA certificate from the official Mosquitto test broker:
wget https://test.mosquitto.org/ssl/mosquitto.org.crt -O mosquitto_ca.crt
Publish a message over TLS
./samples/bin/mqtt_client \
--mqtt_servername ${MQTT_SERVER:-test.mosquitto.org} \
--mqtt_port ${MQTT_SSL_PORT:-8883} \
--mqtt_transport SSL \
--ssl_ca_file mosquitto_ca.crt \
--mqtt_pub_topic demo/topic \
--mqtt_pub_message "Hello NanoMQTT" \
--mqtt_clean_start