Overview
The Invernaderos API uses a hierarchical MQTT topic structure for organizing sensor data, actuator commands, and system events. Topics support multi-tenant isolation and follow MQTT best practices for scalability.Topic Hierarchy
The system uses the following topic pattern:Root Topics
Legacy Format - Maps to
DEFAULT tenant for backward compatibilityMulti-Tenant Format - Isolates data by tenant
Response Topic - Echo/verification messages
Tenant-Based Topics
The API supports multi-tenant isolation through topic-based routing. Each tenant’s data is segregated using a unique tenant identifier in the topic path.Topic Extraction
The system extracts the tenant ID from the topic using the following logic:Examples
| Topic | Extracted Tenant ID | Description |
|---|---|---|
GREENHOUSE | DEFAULT | Legacy format for backward compatibility |
GREENHOUSE/SARA | SARA | Vivero Sara greenhouse system |
GREENHOUSE/001 | 001 | Generic tenant ID |
GREENHOUSE/NARANJOS | NARANJOS | Los Naranjos farm |
The
DEFAULT tenant is automatically created during database migration (V7) to support existing systems using the legacy GREENHOUSE topic.Sensor Topics
Sensor data is published to tenant-specific topics following this pattern:Message Format
Subscription Pattern
To subscribe to all greenhouse data across tenants, use the wildcard pattern:Actuator Topics
Actuator commands follow a more specific topic hierarchy:Command Format
Actuator commands should use QoS 1 (at least once) to ensure reliable delivery.
Alert Topics
System alerts are published to tenant-specific alert topics:Alert Types
Sensor reading exceeded configured threshold
Sensor has not reported within expected interval
Actuator failed to respond to command
System-level error requiring attention
Alert Format
System Event Topics
System-wide events are published to the following topics:Event Types
system/events/startup- API startedsystem/events/shutdown- API shutting downsystem/events/connection- MQTT connection status changessystem/events/error- System-level errors
Topic-Based Routing
The system uses Spring Integration’s message routing to direct MQTT messages to appropriate handlers:Routing Table
| Topic Pattern | Handler | QoS | Description |
|---|---|---|---|
GREENHOUSE | GreenhouseDataListener | 0 | Legacy greenhouse data |
GREENHOUSE/+ | GreenhouseDataListener | 0 | Multi-tenant greenhouse data |
greenhouse/+/sensors/# | SensorDataListener | 0 | Individual sensor readings |
greenhouse/+/actuators/# | ActuatorStatusListener | 1 | Actuator status updates |
system/events/# | SystemEventListener | 0 | System events |
Wildcard Subscriptions
MQTT supports two types of wildcards:Single-Level Wildcard (+)
Matches exactly one topic level:Multi-Level Wildcard (#)
Matches zero or more topic levels (must be at the end):Response Topic (Echo)
The API automatically echoes received messages to theSYSTEM/RESPONSE topic for verification:
- Publish sensor data to
GREENHOUSE/{tenantId} - Subscribe to
SYSTEM/RESPONSE - Verify the API received and processed the data correctly
Best Practices
Topic Naming
- Use descriptive, hierarchical topic names
- Avoid spaces and special characters
- Use forward slashes (/) to separate levels
- Keep topic depth reasonable (3-5 levels max)
QoS Selection
- QoS 0: Sensor data (frequent, loss acceptable)
- QoS 1: Actuator commands (must be received)
- QoS 2: Critical alerts (no duplicates)
Multi-Tenancy
- Always include tenant ID in topics for isolation
- Use the
DEFAULTtenant for legacy compatibility - Validate tenant IDs in message handlers
Security
- Implement ACL rules to restrict topic access per tenant
- Use authentication for all MQTT clients
- Avoid publishing sensitive data in topic names
The MQTT broker (EMQX) supports Access Control Lists (ACLs) to restrict which clients can publish/subscribe to specific topics. Configure ACLs to enforce tenant isolation.