← 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.
- Sensor readings, command topics, and status messages can be visible to unrelated subscribers
- Anyone who can reach the broker may be able to publish fake commands to predictable topics
- Retained messages and topic collisions can leak state or mix unrelated test traffic into a real deployment
Why This Happens:
- 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.
- False security: Topic names like
plant/boiler7/temperaturefeel private but are completely visible - 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:
- Public broker: useful for learning, but no project-specific privacy, authentication, or uptime accountability
- Private broker: more setup work, but lets you control users, TLS, ACLs, logs, and maintenance windows
- Managed cloud IoT broker: shifts more operations to the provider, but still requires correct device identity and topic policy design
Production Checklist:
- Private broker (not test.mosquitto.org)
- TLS encryption (port 8883, not 1883)
- Authentication (username/password minimum)
- ACLs (topic-level permissions)
- Monitoring (detect unauthorized access)