A complete Django application that subscribes to MQTT messages from ControlByWeb devices and broadcasts them to a Next.js frontend via WebSocket in real-time.
ControlByWeb Device (MQTT Publisher)
│
▼
MQTT Broker
│
▼
Django Backend (MQTT Subscriber)
├── MQTT Client (paho-mqtt)
├── Django Channels (WebSocket)
└── REST API (DRF)
│
▼
Next.js Frontend (WebSocket Client)
- Python 3.8+
- Node.js 18+
- Redis
macOS:
brew install redis
brew services start redis
redis-cli ping # Should return PONGLinux:
sudo apt-get install redis-server
sudo systemctl start redis
redis-cli pingcd backend
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
# Create .env file and configure MQTT broker settings:
# MQTT_BROKER_HOST=fd6ad94a23dc48f7aadb2a1c9b3f5c65.s1.eu.hivemq.cloud
# MQTT_BROKER_PORT=8883
# MQTT_USERNAME=test1234
# MQTT_PASSWORD=Test1234
# MQTT_TOPICS=flash/sirens/+/status
# MQTT_USE_TLS=True
# MQTT_TLS_INSECURE=True
python manage.py migrate
python manage.py runserverBackend runs at: http://localhost:8000
cd frontend
npm install
npm run devFrontend runs at: http://localhost:3000
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000/api/mqtt/status/
- Swagger UI: http://localhost:8000/swagger/
- Admin Panel: http://localhost:8000/admin/
Import postman_collection.json into Postman, or import directly from:
- Swagger JSON: http://localhost:8000/swagger.json
GET /api/mqtt/status/- Get MQTT connection status
GET /api/mqtt/messages/- List messages (paginated)GET /api/mqtt/messages/?topic=<topic>- Filter by topicGET /api/mqtt/messages/<id>/- Get specific messagePOST /api/mqtt/clear-history/- Clear message history
GET /api/mqtt/config/- List configurationsPOST /api/mqtt/config/- Create configurationGET /api/mqtt/config/<id>/- Get configurationPUT /api/mqtt/config/<id>/- Update configurationPATCH /api/mqtt/config/<id>/- Partial updateDELETE /api/mqtt/config/<id>/- Delete configuration
GET /swagger/- Swagger UIGET /redoc/- ReDoc documentationGET /swagger.json- OpenAPI JSON schema
- Endpoint:
ws://localhost:8000/ws/mqtt/ - Message Format: JSON
{
"type": "mqtt_message",
"data": {
"topic": "device/temperature",
"payload": {"temperature": 25.5},
"qos": 0,
"timestamp": "2024-01-01T12:00:00Z"
}
}{
"type": "ping"
}- Verify MQTT broker is running and accessible
- Check credentials in backend
.env - Review backend logs for errors
- Ensure Redis is running:
redis-cli ping - Check Redis host/port in backend
.env
- Ensure backend is running
- Check WebSocket URL in frontend
.env.local - Check browser console for errors
Use the provided test publisher script:
# Install paho-mqtt if not already installed
pip install paho-mqtt
# Run test publisher
python test_publisher.pyThis will publish test messages to the MQTT broker. Check your Django backend logs and frontend to see the messages appear in real-time.
To verify messages are being published:
python test_subscriber.py- Host:
fd6ad94a23dc48f7aadb2a1c9b3f5c65.s1.eu.hivemq.cloud - Port:
8883(TLS) - Username:
test1234 - Password:
Test1234
For detailed publisher setup and ControlByWeb device configuration, see:
MQTT_PUBLISHER_GUIDE.md- Complete MQTT publishing guide
MQTT_WebSocket_Django/
├── backend/ # Django backend (see backend/README.md)
├── frontend/ # Next.js frontend (see frontend/README.md)
├── test_publisher.py # Test script to publish MQTT messages
├── test_subscriber.py # Test script to subscribe to MQTT messages
├── postman_collection.json
└── MQTT_PUBLISHER_GUIDE.md # MQTT publishing guide
For detailed setup instructions, see:
backend/README.md- Backend documentationfrontend/README.md- Frontend documentationMQTT_PUBLISHER_GUIDE.md- MQTT publishing and ControlByWeb configuration