Skip to content

MQTT Tutorials

The tutorials below are a collection of step-by-step, hands-on resources meant to steadily build skills with MQTT. Get a broker running first — nothing below works without one to connect to.

You don’t need an account or a cloud service to try MQTT — a broker on localhost or a public test broker is enough.

  • Install Eclipse Mosquitto locally (Ubuntu/Debian):

    Terminal window
    sudo apt update
    sudo apt install mosquitto mosquitto-clients
  • Or use a public test broker — no install required, good for a first experiment: test.mosquitto.org (port 1883, unencrypted/unauthenticated).

    Terminal window
    # Terminal 1 — subscribe
    mosquitto_sub -h test.mosquitto.org -t "pacbot/test"
    # Terminal 2 — publish
    mosquitto_pub -h test.mosquitto.org -t "pacbot/test" -m "hello mqtt"

Eclipse Paho is the official MQTT client project under the Eclipse Foundation, and paho-mqtt is its Python client — the library pip installs most often for MQTT in Python.

Terminal window
pip install paho-mqtt

Subscriber:

import paho.mqtt.client as mqtt
from paho.mqtt.enums import CallbackAPIVersion
def on_connect(client, userdata, flags, reason_code, properties):
print(f"Connected with result code {reason_code}")
client.subscribe("pacbot/test")
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
mqttc = mqtt.Client(CallbackAPIVersion.VERSION2)
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.connect("test.mosquitto.org", 1883, 60)
mqttc.loop_forever()

Publisher:

import paho.mqtt.client as mqtt
from paho.mqtt.enums import CallbackAPIVersion
mqttc = mqtt.Client(CallbackAPIVersion.VERSION2)
mqttc.connect("test.mosquitto.org", 1883, 60)
mqttc.loop_start()
mqttc.publish("pacbot/test", "hello from python", qos=1)
mqttc.loop_stop()
mqttc.disconnect()

paho.mqtt.cpp is Paho’s C++ wrapper around the underlying paho.mqtt.c library.

Build from source (Ubuntu/Debian):

Terminal window
sudo apt-get install build-essential gcc make cmake libssl-dev
git clone https://github.com/eclipse/paho.mqtt.cpp
cd paho.mqtt.cpp
git submodule update --init
cmake -Bbuild -H. -DPAHO_WITH_MQTT_C=ON -DPAHO_BUILD_EXAMPLES=ON
sudo cmake --build build/ --target install

Publisher (mqtt::client, synchronous API — adapted from the official sync_publish.cpp example):

#include <iostream>
#include "mqtt/client.h"
const std::string SERVER_URI{"tcp://test.mosquitto.org:1883"};
const std::string TOPIC{"pacbot/test"};
const int QOS = 1;
int main() {
mqtt::client client(SERVER_URI, "");
mqtt::connect_options connOpts;
connOpts.set_keep_alive_interval(20);
connOpts.set_clean_session(true);
try {
client.connect(connOpts);
auto msg = mqtt::make_message(TOPIC, "hello from c++");
msg->set_qos(QOS);
client.publish(msg);
client.disconnect();
}
catch (const mqtt::exception& exc) {
std::cerr << exc.what() << std::endl;
return 1;
}
return 0;
}

Subscriber (adapted from the official sync_consume.cpp example):

#include <iostream>
#include "mqtt/client.h"
const std::string SERVER_URI{"tcp://test.mosquitto.org:1883"};
const std::string TOPIC{"pacbot/test"};
int main() {
mqtt::client client(SERVER_URI, "pacbot_cpp_subscriber");
try {
client.connect();
client.subscribe(TOPIC, 1);
while (true) {
auto msg = client.consume_message();
if (msg) {
std::cout << msg->get_topic() << ": " << msg->to_string() << std::endl;
}
}
}
catch (const mqtt::exception& exc) {
std::cerr << exc.what() << std::endl;
return 1;
}
return 0;
}

All the best 👍 !!