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 nanomqtt from projects/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 flagPurpose
-DENABLE_MQTT_STREAMING=ONEnables MQTT streaming mode
-DENABLE_MQTT_UNITTEST=ONEnables MQTT client unit tests
-DENABLE_MQTT_TEST=ONEnables 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

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_start

Get 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

What’s next?