Skip to main content

Overview

AppManager is responsible for the complete lifecycle of third-party apps within a user session. It handles app starting, stopping, connection management, webhook triggering, and state tracking. This manager encapsulates all app-related functionality that was previously scattered throughout the codebase. File: packages/cloud/src/services/session/AppManager.ts

Key Responsibilities

  1. App Lifecycle Management: Starting, stopping, and restarting apps
  2. Connection Management: WebSocket connections and heartbeat monitoring
  3. State Tracking: Connection states, pending operations, and app status
  4. Webhook Communication: Triggering app webhooks for session initialization
  5. Hardware Compatibility: Checking app requirements against device capabilities
  6. Grace Period Handling: Managing reconnections during network interruptions

State Management

Connection States

Internal State

App Starting Process

Start Flow

Implementation

Webhook Triggering

Connection Handling

App Initialization

When an app connects via WebSocket:

Heartbeat System

Maintains connection health:

App Stopping

Stop Process

Grace Period Handling

When an app disconnects unexpectedly:

Message Handling

Send Message to App

Cleanup

Disposal

Environment Configuration

AppManager adapts WebSocket URLs based on environment:
  • Local Development: ws://localhost:8002/app-ws
  • Docker Compose: ws://cloud/app-ws (for system apps)
  • Kubernetes (Porter): ws://${APP_NAME}-cloud.default.svc.cluster.local:80/app-ws
  • Production: wss://${CLOUD_PUBLIC_HOST_NAME}/app-ws

Best Practices

  1. Always check hardware compatibility before starting apps
  2. Handle connection timeouts to prevent hanging operations
  3. Implement grace periods for network interruptions
  4. Track app metrics for usage analytics
  5. Clean up resources when apps stop
  6. Use appropriate URLs based on environment and app type