User Identification in MQTT Events
Overview
When sending data through MQTT, you can include user identification information in your messages to track which user or device originated the data.
Message Structure with User ID
{
"user_id": "user@example.com",
"device_id": "device-123",
"timestamp": "2024-01-15T12:00:00Z",
"data": {
"temperature": 25.5,
"humidity": 60,
"pressure": 1013.25
}
}
Field Descriptions
| Field | Type | Required | Description |
|---|---|---|---|
user_id | string | Yes | Email or unique identifier of the user |
device_id | string | Yes | Unique device identifier |
timestamp | string | Yes | ISO 8601 timestamp |
data | object | Yes | Sensor data payload |
Best Practices
- Always include
user_idfor proper data attribution - Use consistent
device_idformat across your organization - Ensure timestamps are in UTC
- Validate data before sending
Example
import paho.mqtt.client as mqtt
import json
from datetime import datetime
# MQTT Configuration
broker = "mqtt.grouplink.com.br"
port = 8883
topic = "data/sensors"
# Create message with user identification
message = {
"user_id": "john.doe@company.com",
"device_id": "sensor-001",
"timestamp": datetime.utcnow().isoformat() + "Z",
"data": {
"temperature": 22.5,
"humidity": 55
}
}
# Publish
client = mqtt.Client()
client.connect(broker, port)
client.publish(topic, json.dumps(message))
client.disconnect()