SDK MQTT Topic and Session UUID Changes
Date: 2025-11-12 Status: ✅ Complete - Ready for TestingOverview
This document details the SDK changes required to support the unified MQTT topic structure and backend-generated session UUIDs.What Changed
1. Dynamic MQTT Topics
Before:2. Session Creation Workflow
Before:Files Modified
Header File: DeviceManager.hpp
Location:packages/roboticks-device/include/roboticks/device/DeviceManager.hpp
Changes:
- Added Member Variables (lines 451-464):
- Added Method Declaration (line 325):
Implementation File: DeviceManager.cpp
Location:packages/roboticks-device/src/DeviceManager.cpp
Changes:
- Updated Topic Constants (lines 42-53):
- Dynamic Topic Construction (lines 147-155, 217-225):
- Subscribe to Session Response Topic (lines 178-199):
- Session Response Handler (lines 1068-1113):
- Refactored createSession Method (lines 1119-1215):
- Check IoT connection before starting
- Set waiting flag and publish request WITHOUT session_id
- Wait for backend response with 30-second timeout
- Create Session object only after receiving backend UUID
- Return nullptr if timeout or error
- Updated All MQTT Publish Calls:
- Heartbeat:
iot_client_->publish(heartbeat_topic_, ...) - Sessions:
iot_client_->publish(sessions_topic_, ...) - Logs:
iot_client_->publish(logs_topic_, ...)
Key Behavior Changes
Session Creation Flow
Old Behavior:- Device calls
createSession() - Device generates UUID locally
- Device creates Session object immediately
- Device publishes session info to MQTT
- Backend stores session with device UUID
- Device calls
createSession() - Device publishes MQTT request WITHOUT session_id
- Device waits for backend response (blocking call)
- Backend generates UUID via PostgreSQL
- Backend publishes UUID back to device via
session/responsetopic - Device receives UUID and creates Session object
- Session ready for use
Timeout Handling
If backend doesn’t respond within 30 seconds:createSession()returnsnullptr- Logs error: “Timeout waiting for session creation response”
- Application must handle null session pointer
Topic Isolation
Each device now has its own topic namespace:Testing Instructions
Build SDK
Test Session Creation
Verify MQTT Traffic
Monitor CloudWatch logs for: Device publishes to:roboticks/fleet/{device_id}/sessions
roboticks/fleet/{device_id}/session/response
Expected Logs
Error Scenarios
1. Backend Timeout
2. Connection Lost
3. Invalid Response
Migration Notes
Breaking Changes
-
Session Creation is Now Blocking
- Old:
createSession()returned immediately - New:
createSession()blocks up to 30 seconds
- Old:
-
Session Creation Can Fail
- Old: Always returned a Session object
- New: Returns
nullptron timeout/error
-
Application Must Check for Null
Backward Compatibility
Not backward compatible with old backend infrastructure:- Devices with updated SDK require updated backend
- Old SDK devices will fail with new backend (403 errors)
- Gradual rollout recommended: update backend first, then devices
Performance Impact
- Session Creation Latency: +100-150ms (backend round-trip)
- Heartbeat/Logs: No change (already async)
- Memory: +7 string member variables per DeviceManager instance
Rollback Plan
If issues arise with SDK:-
Revert to previous SDK version:
- Backend must also be rolled back (topics are incompatible)