Skip to main content

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

  1. Session Lifecycle Management: Creating and retrieving sessions
  2. Message Routing: Relaying messages between glasses and apps
  3. Settings Management: User and app-specific settings
  4. Session Transformation: Preparing session data for clients
  5. 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

  1. Always check WebSocket state before sending messages
  2. Handle reconnections gracefully by preserving session state
  3. Return sensible defaults when data is unavailable
  4. Log errors with context for debugging
  5. Delegate specialized tasks to appropriate managers