MQTT Pub/Sub Animation
Interactive Visualization of Publish-Subscribe Messaging
MQTT Publish-Subscribe Flow
This interactive animation demonstrates how MQTT’s publish-subscribe pattern works. Watch messages flow from publishers through the broker to subscribers based on topic matching.
This animation shows the core MQTT concepts:
- Publishers (left): Sensors that send data to topics
- Broker (center): Routes messages based on topic patterns
- Subscribers (right): Apps that receive messages matching their subscriptions
- Topic Wildcards:
#matches all levels,+matches one level
- Click “Publish Message” buttons on the left to send messages
- Watch the message travel through the broker
- See which subscribers receive it based on their topic subscriptions
- Try different message types to understand topic routing
Topic Subscription Reference
The animation above demonstrates topic-based routing. Here’s what each subscriber receives:
| Subscriber | Subscribed Topics | Receives From |
|---|---|---|
| 📱 Phone App | home/# |
ALL sensors (wildcard # matches everything) |
| 🌡️ Thermostat | home/+/temperature |
Temperature sensor only (+ matches single level) |
| 🔒 Security Hub | home/garage/motion, home/frontdoor/status |
Motion + Door sensors (exact match) |
| 📊 Data Logger | home/livingroom/# |
Temperature sensor only (living room subtree) |
#(Multi-level): Matches zero or more topic levels.home/#matcheshome/kitchen/temperature,home/garage/motion/sensor1, etc.+(Single-level): Matches exactly one topic level.home/+/temperaturematcheshome/kitchen/temperaturebut NOThome/kitchen/sensor/temperature
What This Animation Demonstrates
- Decoupling: Publishers don’t know (or care) who receives their messages
- Topic Routing: The broker routes messages based on topic patterns
- Wildcard Subscriptions:
#and+wildcards enable flexible subscriptions - Fan-out: One message can reach multiple subscribers simultaneously
- Selective Delivery: Subscribers only receive messages matching their topics
Technical Implementation Notes
This animation uses:
- Observable JavaScript (OJS): Native to Quarto, reactive programming model
- D3.js: SVG manipulation and smooth transitions
- CSS-in-JS: Inline styles for portability
- IEEE Color Palette: Consistent with book styling (#2C3E50, #16A085, #E67E22)
The animation is self-contained—no external dependencies beyond D3.js (included with Quarto).
- Click Temperature Sensor → Watch the Phone App AND Thermostat AND Data Logger all light up
- Click Motion Sensor → Only Phone App and Security Hub receive it
- Click Door Sensor → Only Phone App and Security Hub receive it
Notice how the Phone App always receives messages (it subscribed to home/#), while other subscribers are more selective.
What’s Next
- MQTT QoS Levels - Add reliability guarantees to this flow
- MQTT Labs - Build your own pub/sub system
- CoAP Comparison - See how request/response differs
- Simulations Hub - More interactive visualizations
Pitfall 1: Using Wildcards in PUBLISH Operations Wildcards (# and +) are only valid in SUBSCRIBE operations. Publishing to home/+/temperature does not work - you must publish to a specific topic like home/livingroom/temperature. The broker will reject wildcard characters in publish topic names.
Pitfall 2: Creating Overly Deep or Flat Topic Hierarchies Topics like a/b/c/d/e/f/g/h/sensor/temp waste bandwidth and are hard to manage. Conversely, flat topics like sensor1_temp_livingroom prevent wildcard subscriptions. Aim for 3-5 levels that balance structure with practicality: building/zone/device/metric.
Pitfall 3: Starting Topics with / (Leading Slash) A topic like /home/temp is valid but creates an empty first level. This means home/# will NOT match /home/temp because they have different structures. Either always use leading slashes or never use them - be consistent.
Pitfall 4: Forgetting That Subscription is Stateful Once subscribed, a client continues receiving messages until it unsubscribes or disconnects. Subscribing multiple times to the same topic (e.g., in a reconnection loop without checking) can cause duplicate message delivery or exceed broker subscription limits.
- Publishers and subscribers are fully decoupled: Sensors publish to topics without knowing who receives messages, and apps subscribe without knowing which devices exist - the broker handles all routing.
- Topic wildcards enable flexible subscriptions: Use
#to match any number of levels (e.g.,home/#receives all home messages) or+to match exactly one level (e.g.,home/+/temperature). - One publish can reach many subscribers: The broker duplicates messages to all matching subscriptions, enabling efficient fan-out from a single sensor to multiple applications.
- Topic hierarchy design is critical: A well-structured topic tree (e.g.,
building/floor/room/sensor/type) enables both fine-grained and broad subscriptions while keeping the namespace organized.
This animation is implemented in approximately 300 lines of Observable JavaScript. Key techniques:
- SVG-based graphics: Scalable, accessible, print-friendly
- D3 transitions: Smooth easing with
d3.easeQuadInOut - Reactive state: OJS mutable variables for message queue
- Topic matching: JavaScript implementation of MQTT wildcard patterns
To reuse this pattern for other protocols (CoAP, AMQP, etc.), the core animation engine can be abstracted into a reusable component.