MQTT Live Updates¶
For real-time data push — IoT sensors, live monitoring, streaming data.
When to Use MQTT vs Polling¶
| Polling | MQTT | |
|---|---|---|
| Latency | Seconds (interval-based) | Milliseconds (push) |
| Infrastructure | None (just HTTP) | MQTT broker required |
| Use case | Dashboards, reports | IoT, live monitoring |
| Complexity | Simple | Moderate |
Use polling unless you need sub-second updates or already have an MQTT broker.
Setup¶
1. Enable in Django settings¶
2. Create a live chart¶
from d3_bridge import LineChart
chart = LineChart(
data=SensorReading.objects.last_hour().values("timestamp", "temperature"),
x="timestamp",
y="temperature",
title="Temperature (Live)",
live=True,
mqtt_broker="ws://your-broker:8083/mqtt",
mqtt_topic="sensors/+/temperature",
mqtt_window=100, # keep last 100 data points
)
3. MQTT message format¶
The broker should publish JSON matching your chart's data fields:
How It Works¶
Django View (initial data) ──→ Chart renders
↑
MQTT Broker (WebSocket) ──→ mqtt.js ──→ chart.update(newPoint)
- Chart renders with initial data from the Django view
- JavaScript connects to the MQTT broker via WebSocket
- On each message,
chart.update()is called with the new data point - The chart re-renders with a D3 transition
Connection Status¶
A live indicator appears in the top-right corner of the chart:
- LIVE (green) — connected and receiving
- LIVE (red) — disconnected, attempting to reconnect
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
live |
bool | False |
Enable MQTT |
mqtt_broker |
str | None |
WebSocket URL (ws:// or wss://) |
mqtt_topic |
str | None |
MQTT topic (supports + and # wildcards) |
mqtt_window |
int | 100 |
Max data points to keep |
mqtt_qos |
int | 0 |
MQTT QoS level (0, 1, or 2) |
Compatible Brokers¶
Any MQTT broker with WebSocket support: