Skip to main content

Overview

The WebSocket Service is the central hub for all real-time communication in MentraOS Cloud. It manages two separate WebSocket servers - one for smart glasses connections and another for third-party app connections. File: packages/cloud/src/services/websocket/websocket.service.ts

Architecture

Key Components

Singleton Pattern

The service implements a singleton pattern to ensure only one instance manages all WebSocket connections:

Connection Routing

The service handles HTTP upgrade requests and routes them based on the URL path:
  1. /glasses-ws - Routes to glasses WebSocket handler
  2. /app-ws - Routes to app WebSocket handler
  3. Unknown paths - Rejects connection

Authentication Flow

Glasses Authentication

For glasses connections (/glasses-ws):
  1. Token Extraction: JWT token from Authorization header
  2. Token Verification: Using AUGMENTOS_AUTH_JWT_SECRET
  3. User ID Extraction: From JWT payload (userData.email)
  4. Request Enhancement: Attaches userId to request object

App Authentication

For app connections (/app-ws):
  1. JWT Authentication (preferred):
    • Bearer token in Authorization header
    • Headers: x-user-id and x-session-id required
    • JWT payload contains packageName and apiKey
  2. Request Enhancement: Attaches to request:
    • userId - From x-user-id header
    • sessionId - From x-session-id header
    • appJwtPayload - Decoded JWT payload

Connection Handlers

Glasses Connection Handler

App Connection Handler

App Lifecycle Messages

The service provides methods to notify glasses when apps start or stop:

sendAppStarted

Notifies glasses when an app begins running:

sendAppStopped

Notifies glasses when an app stops:

Error Handling

Authentication Errors

For glasses connections:
For app connections:

HTTP Response Codes

  • 401 Unauthorized: Invalid or missing authentication
  • Connection closed: Unknown WebSocket path

Server Setup

The service attaches to an HTTP server using the setupWebSocketServers method:

Logging

Comprehensive logging includes:
  • Connection attempts with headers and paths
  • Authentication success/failure
  • Connection establishment
  • Message sending status
  • Error details with stack traces

Environment Variables

  • AUGMENTOS_AUTH_JWT_SECRET: Secret key for JWT token verification (required)

Best Practices

  1. Connection State Checks: Always verify WebSocket readyState before sending
  2. Error Handling: Catch and log all handler errors
  3. Token Security: Never log full tokens, only first 10 characters
  4. Request Enhancement: Attach authentication data to request for downstream handlers