← Back to the chapter · Previous detail · Next detail →

Technical detail · MQTT Python: Reliable Client Patterns

Public-broker exposure and a Mosquitto deployment example

An open public MQTT broker can expose device readings and commands.

Mosquitto routes MQTT messages. TLS (Transport Layer Security) protects connections; access-control lists (ACLs) set topic permissions. In slash-separated topic filters, + matches one level; final # matches zero or more.

The Mistake: Many developers prototype with test.mosquitto.org and then deploy the same code to production, assuming a unique topic name provides adequate security.

Production impact:

On an open public broker, an outsider can read the shut-off valve command on plant/boiler7/valve/set and publish a replayed command.

Why This Happens:

  1. Developers don’t realize: On the unauthenticated test listener, another user can subscribe with +/# to read ordinary application topics and commands. It does not match topics beginning with $. Blocking only a literal # subscription does not protect messages.
  2. False security: Topic names like plant/boiler7/temperature feel private but are completely visible
  3. Deadline pressure: Teams keep a prototype broker because the first demo worked

The Fix:

# Install Mosquitto (Ubuntu/Debian)
sudo apt install mosquitto mosquitto-clients

# Create user
sudo mosquitto_passwd -c /etc/mosquitto/passwd admin

# Enable TLS with a certificate provider such as Let's Encrypt
sudo certbot certonly --standalone -d mqtt.example.com

# Configure security
sudo nano /etc/mosquitto/mosquitto.conf
# Add:
#   listener 8883
#   certfile /etc/letsencrypt/live/mqtt.example.com/fullchain.pem
#   keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
#   allow_anonymous false
#   password_file /etc/mosquitto/passwd

# Restart broker
sudo systemctl restart mosquitto

Deployment trade-off:

Production Checklist:

← Back to the chapter · Previous detail · Next detail →