Overview
The Session Service is the central coordinator for user session management in MentraOS Cloud. It handles session creation, retrieval, and transformation while delegating specialized tasks to the UserSession class and its managers. File:packages/cloud/src/services/session/session.service.ts
Key Responsibilities
- Session Lifecycle Management: Creating and retrieving sessions
- Message Routing: Relaying messages between glasses and apps
- Settings Management: User and app-specific settings
- Session Transformation: Preparing session data for clients
- Audio Routing: Directing audio data to appropriate handlers
Session Creation
The service creates or retrieves sessions with automatic reconnection handling:Reconnection Handling
When a user reconnects:- Existing session is preserved
- WebSocket connection is updated
- Cleanup timers are cancelled
- Disconnection timestamp is cleared
Session Retrieval
Multiple methods for accessing sessions:Session Transformation
Prepares session data for client consumption with calculated requirements:Message Routing
Relay to Apps
Routes messages from glasses to subscribed apps:Audio Routing
Delegates audio processing to the AudioManager:Audio Response Routing
Routes audio play responses back to requesting apps:Settings Management
User Settings
Retrieves combined system and app settings:App-Specific Settings
Default Settings
System default settings when user settings are not available:Error Handling
- Session Creation: Logs errors and re-throws for upstream handling
- Message Routing: Catches and logs send errors per app
- Settings Retrieval: Returns defaults on error
- Transformation: Returns minimal data on error
Integration Points
- UserSession: Core session object creation and management
- SubscriptionService: Determining app subscriptions and requirements
- AppService: Fetching installed apps
- User Model: Database access for settings
- AudioManager: Audio data processing
- MicrophoneManager: Microphone state management
Best Practices
- Always check WebSocket state before sending messages
- Handle reconnections gracefully by preserving session state
- Return sensible defaults when data is unavailable
- Log errors with context for debugging
- Delegate specialized tasks to appropriate managers
Related Documentation
- UserSession Class: Core session implementation
- SubscriptionService: Subscription management
- AudioManager: Audio processing
- WebSocketService: Connection management