Your Command Center: UserSessions
Remember Alex from our authentication story? Now that they’re connected, the cloud creates a UserSession - their personal command center. Think of it as a smart assistant that manages everything happening with their glasses.What is a UserSession?
A UserSession is the heart of a user’s experience in MentraOS Cloud. It’s a long-lived object that:- Maintains the WebSocket connection to the glasses
- Manages all the specialized services (managers)
- Tracks which apps are running
- Handles all message routing
- Survives temporary disconnections
Meet the Managers
Think of managers as specialized departments in Alex’s personal cloud office. Each one handles a specific type of functionality:🎯 AppManager - The App Orchestrator
Manages the lifecycle of third-party apps:- Starting and stopping apps
- Monitoring app health with heartbeats
- Handling app reconnections
- Managing the 20-second grace period when apps disconnect
- Finds the translator app in installed apps
- Notifies the app via webhook to start
- Waits for app to connect via WebSocket
- Monitors the app’s health
🎤 AudioManager - The Sound Department
Handles all things audio:- Receiving audio streams from glasses microphone
- Buffering audio data for processing
- Sending audio to glasses speakers
- Managing audio format conversions
📺 DisplayManager - The Visual Controller
Controls what appears on the glasses display:- Rendering layouts from apps
- Enforcing the 200-300ms throttle between updates
- Managing display queues
- Preventing display spam
🎙️ TranscriptionManager - The Speech-to-Text Expert
Converts speech to text in real-time:- Managing transcription providers (Deepgram, Assembly, etc.)
- Streaming audio to transcription services
- Handling multiple language streams
- Distributing transcripts to subscribed apps
📸 PhotoManager - The Camera Controller
Handles photo capture:🌐 MicrophoneManager - The Audio Gate
Controls the glasses microphone:📊 DashboardManager - The Always-On Display
Manages the dashboard mode (always-visible information):How Managers Work Together
Let’s see how managers collaborate when Alex says “Take a photo”:- AudioManager receives the audio stream
- TranscriptionManager converts it to text: “Take a photo”
- AppManager routes the transcript to subscribed apps
- App recognizes the command and tells PhotoManager
- PhotoManager sends photo request to glasses
- Glasses capture photo and send it back
- PhotoManager receives photo and gives it to the app
- App processes photo and uses DisplayManager to show “Photo saved!”
Session Lifecycle
Birth: Session Creation
When Alex connects:Life: Active Session
During normal operation:- Heartbeat runs every 10 seconds
- Messages flow between glasses and apps
- Managers handle their specific duties
- Everything is logged for debugging
Near-Death: Disconnection
When connection drops:- 30-second grace period begins
- Apps enter “resurrecting” state
- Session waits for reconnection
- No data is lost
Resurrection or Death
If Alex reconnects within 30 seconds:- WebSocket is updated
- Apps are notified
- Everything continues normally
- Session is cleaned up
- All managers are destroyed
- Apps are notified of session end
- Memory is freed
Session Storage
All active sessions live in SessionStorage:Best Practices
- Always check session exists before using it
- Use the appropriate manager for each task
- Let managers handle their domain - don’t bypass them
- Clean up resources when sessions end
- Log important events for debugging