AppSession
AppSession
(also known as TpaSession
in older versions) manages an active WebSocket connection (session) between an app instance and MentraOS Cloud. It handles event subscriptions, layout display, and connection management for a single user session.
Constructor
config
: Configuration options for the app session
Properties
events
Provides access to theEventManager
for subscribing to real-time events.
layouts
Provides access to theLayoutManager
for controlling the AR display.
settings
Provides access to theSettingsManager
for reading and monitoring app settings.
dashboard
Provides access to theDashboardAPI
for sending content to the user’s dashboard.
capabilities
Provides access to the device capabilities of the connected smart glasses.logger
Provides access to a pre-configured Pino logger instance for session-specific logging.userId
: The current user’s identifierpackageName
: Your app’s package namesessionId
: The current session identifierservice
: Set to ‘app-session’
session.logger.debug()
: Detailed debugging informationsession.logger.info()
: General information about app operationsession.logger.warn()
: Warning conditions that don’t stop executionsession.logger.error()
: Error conditions that should be investigated
Event Handling Methods
onTranscription()
Registers a handler for real-time speech transcription events.handler
: Callback function to processTranscriptionData
onHeadPosition()
Registers a handler for head position change events (e.g., ‘up’, ‘down’).handler
: Callback function to processHeadPosition
data
onButtonPress()
Registers a handler for hardware button press events on the glasses.handler
: Callback function to processButtonPress
data
onPhoneNotifications()
Registers a handler for notifications received from the connected phone.handler
: Callback function to processPhoneNotification
data
Subscription Methods
subscribe()
Informs the MentraOS Cloud that this app session wants to receive events of the specified type.type
: TheStreamType
to subscribe to
on()
Generic method to subscribe to any data stream type. Use specificon<EventType>
methods where available.
event
: TheStreamType
to listen forhandler
: Callback function to process the data associated with the stream type
Connection Methods
connect()
Establishes the WebSocket connection to MentraOS Cloud for this session.sessionId
: The unique identifier for this session (provided by theSESSION_REQUEST
webhook)
disconnect()
Gracefully closes the WebSocket connection and cleans up resources for this session.Settings Methods
getSettings()
Retrieves all current application settings for this user session.AppSettings
getSetting()
Retrieves the value of a specific application setting by its key.key
: The key of the setting to retrieve
setSubscriptionSettings()
Configures the app session to automatically manage subscriptions based on changes to specific settings.options
: Configuration objectoptions.updateOnChange
: An array of setting keys that should trigger a subscription update when their value changesoptions.handler
: A function that takes the currentAppSettings
and returns an array ofStreamType
subscriptions that should be active