App Lifecycle
This document describes the lifecycle of an MentraOS app within the MentraOS ecosystem. Understanding this lifecycle is crucial for building robust and responsive apps.
Stages of the App Lifecycle​
An MentraOS app goes through the following stages:
-
Registration (One-time): This happens outside of the normal runtime flow. You register your app with MentraOS Developer Portal, providing:
packageName
: A unique identifier (e.g.,com.example.myapp
).name
: A human-readable name.description
: A description of your app.webhookURL
: The URL where MentraOS Cloud will send session start requests.logoURL
: (Optional) URL to your app's logo.apiKey
: A secret key for authenticating your app with the cloud.permissions
: An array of permissions your app needs. See the Permissions guide for details.
This automatically installs the app for your user. For other people to test the app (including others in your organization), they need to install the app. Get the app install link from the App edit page under the
Share with Testers
section. -
Session Request (Webhook): When a user starts your app on their smart glasses, MentraOS Cloud sends an HTTP POST request to your app's
webhookURL
. This request includes:type
:"session_request"
sessionId
: A unique identifier for this session.userId
: The ID of the user who started the app.timestamp
: When the request was sent.
Your app server should listen for these POST requests on the configured
webhookPath
(default:/webhook
). -
WebSocket Connection: Upon receiving the
session_request
, your app establishes a WebSocket connection to MentraOS Cloud. TheAppServer
class in the SDK handles this for you automatically. You provide the cloud's WebSocket URL in theAppServerConfig
:const server = new AppServer({
packageName: PACKAGE_NAME,
apiKey: API_KEY,
port: PORT,
mentraOSWebsocketUrl: `ws://localhost:${CLOUD_PORT}/app-ws`, // Or your cloud URL
webhookPath: '/webhook',
}); -
Connection Initialization: After connecting, your app sends a
tpa_connection_init
message to the cloud. This message includes:type
:"tpa_connection_init"
sessionId
: The session ID from the webhook request.packageName
: Your app's package name.apiKey
: Your app's API key.
The
AppSession
class handles sending this message automatically. -
Subscription: Your app subscribes to the data streams it needs (e.g., transcription, head position) using the
subscribe()
method or theevents
object (see Events for details). This informs MentraOS Cloud which data to send to your app. -
Event Handling: Your app receives real-time events from MentraOS Cloud via the WebSocket connection. You handle these events using event listeners (e.g.,
session.events.onTranscription()
). -
Display Updates: Your app sends display requests to MentraOS Cloud to control what is shown on the glasses' display. You use the
LayoutManager
(accessible throughsession.layouts
) to create and send these requests. -
Session Termination: The session ends when:
- The user stops the app on their glasses.
- The glasses disconnect from the cloud.
- Your app explicitly disconnects.
- An error occurs that terminates the session.
MentraOS Cloud will send a
stop_request
webhook to your app when a session ends. You can override theonStop
method in yourAppServer
to handle any necessary cleanup. TheAppSession
also emits adisconnected
event.
Important Implementation Details​
IMPORTANT: After making changes to your app code or restarting your server, you must restart your app inside the MentraOS phone app.
This restart is necessary because the MentraOS phone app maintains a connection to your cloud app. When you make code changes or restart your server, you need to establish a fresh connection.