> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mentraglass.com/llms.txt
> Use this file to discover all available pages before exploring further.

# EventManager

# EventManager

The `EventManager` handles event subscriptions and dispatching within a [`AppSession`](/app-devs/reference/app-session). It provides methods for subscribing to various event types from the MentraOS platform.

You access the EventManager through the `events` property of a [`AppSession`](/app-devs/reference/app-session) instance:

```typescript theme={null}
const eventManager = appSession.events
```

## Event Handler Methods

### onTranscription()

Registers a handler for real-time speech transcription events.

```typescript theme={null}
onTranscription(handler: (data: TranscriptionData) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`TranscriptionData`](/app-devs/reference/interfaces/event-types#transcriptiondata) objects

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
const unsubscribe = appSession.events.onTranscription(data => {
  console.log(`Transcription: ${data.text}, Final: ${data.isFinal}`)
  if (data.isFinal) {
    // Process the final transcription
  }
})

// Later, to stop receiving events:
unsubscribe()
```

### onHeadPosition()

Registers a handler for head position change events.

```typescript theme={null}
onHeadPosition(handler: (data: HeadPosition) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`HeadPosition`](/app-devs/reference/interfaces/event-types#headposition) objects

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onHeadPosition(data => {
  if (data.position === "up") {
    console.log("User looked up")
  } else if (data.position === "down") {
    console.log("User looked down")
  }
})
```

### onButtonPress()

Registers a handler for hardware button press events on the glasses.

```typescript theme={null}
onButtonPress(handler: (data: ButtonPress) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`ButtonPress`](/app-devs/reference/interfaces/event-types#buttonpress) objects

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onButtonPress(data => {
  console.log(`Button ${data.buttonId} was ${data.pressType} pressed`)
})
```

### onPhoneNotifications()

Registers a handler for notifications received from the connected phone.

```typescript theme={null}
onPhoneNotifications(handler: (data: PhoneNotification) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`PhoneNotification`](/app-devs/reference/interfaces/event-types#phonenotification) objects

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onPhoneNotifications(data => {
  console.log(`Notification from ${data.app}: ${data.title} - ${data.content}`)
})
```

### onGlassesBattery()

Registers a handler for glasses battery update events.

```typescript theme={null}
onGlassesBattery(handler: (data: GlassesBatteryUpdate) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`GlassesBatteryUpdate`](/app-devs/reference/interfaces/event-types#glassesbatteryupdate) objects

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onGlassesBattery(data => {
  console.log(`Glasses battery: ${data.level}%, Charging: ${data.charging}`)
})
```

### onPhoneBattery()

Registers a handler for phone battery update events.

```typescript theme={null}
onPhoneBattery(handler: (data: PhoneBatteryUpdate) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`PhoneBatteryUpdate`](/app-devs/reference/interfaces/event-types#phonebatteryupdate) objects

**Returns:** An unsubscribe function to remove the handler

### onVoiceActivity()

Registers a handler for Voice Activity Detection (VAD) events.

```typescript theme={null}
onVoiceActivity(handler: (data: Vad) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`Vad`](/app-devs/reference/interfaces/event-types#vad-voice-activity-detection) objects

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onVoiceActivity(data => {
  if (data.status === true || data.status === "true") {
    console.log("User is speaking")
  } else {
    console.log("User stopped speaking")
  }
})
```

### onLocation()

An event handler for raw location updates.

Use the [`LocationManager`](/app-devs/reference/managers/location-manager) to access location data, as it provides a simple interface for both streaming and polling.

```typescript theme={null}
onLocation(handler: (data: LocationUpdate) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`LocationUpdate`](/app-devs/reference/interfaces/event-types#locationupdate) objects

**Returns:** An unsubscribe function to remove the handler

### onCalendarEvent()

Registers a handler for calendar event notifications.

```typescript theme={null}
onCalendarEvent(handler: (data: CalendarEvent) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`CalendarEvent`](/app-devs/reference/interfaces/event-types#calendarevent) objects

**Returns:** An unsubscribe function to remove the handler

### onAudioChunk()

Registers a handler for raw audio data chunks. Requires explicit subscription.

```typescript theme={null}
onAudioChunk(handler: (data: AudioChunk) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives [`AudioChunk`](/app-devs/reference/interfaces/event-types#audiochunk) objects

**Returns:** An unsubscribe function to remove the handler

**Note:** Audio chunks require an explicit subscription using appSession.subscribe([StreamType.AUDIO\_CHUNK](/app-devs/reference/enums#streamtype)).

## System Event Handlers

### onConnected()

Registers a handler for when the WebSocket connection is successfully established and authenticated.

```typescript theme={null}
onConnected(handler: (data: AppSettings | undefined) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives optional [`AppSettings`](/app-devs/reference/interfaces/config-types#appsettings) upon connection

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onConnected(settings => {
  console.log("Connected to MentraOS Cloud")
  if (settings) {
    console.log("Initial settings received:", settings)
  }
})
```

### onDisconnected()

Registers a handler for when the WebSocket connection is closed or lost.

```typescript theme={null}
onDisconnected(handler: (reason: string) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives a reason string

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onDisconnected(reason => {
  console.log(`Disconnected from MentraOS Cloud. Reason: ${reason}`)
})
```

### onError()

Registers a handler for WebSocket or SDK-level errors.

```typescript theme={null}
onError(handler: (error: WebSocketError | Error) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives a [`WebSocketError`](/app-devs/reference/interfaces/message-types#websocketerror) or standard Error object

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onError(error => {
  console.error("Error in App session:", error)
})
```

### onSettingsUpdate()

Registers a handler for when the application settings are updated by the user via MentraOS Cloud.

```typescript theme={null}
onSettingsUpdate(handler: (settings: AppSettings) => void): () => void
```

**Parameters:**

* `handler`: Callback function that receives the complete updated [`AppSettings`](/app-devs/reference/interfaces/config-types#appsettings) array

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onSettingsUpdate(settings => {
  console.log("Settings updated:", settings)
})
```

### onSettingChange()

Registers a handler that triggers only when a specific setting's value changes.

```typescript theme={null}
onSettingChange<T>(
  key: string,
  handler: (value: T, previousValue: T | undefined) => void
): () => void
```

**Parameters:**

* `key`: The key of the setting to monitor
* `handler`: Callback function that receives the new value and the previous value (or undefined initially)

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
appSession.events.onSettingChange<boolean>("enableNotifications", (newValue, oldValue) => {
  console.log(`enableNotifications changed from ${oldValue} to ${newValue}`)
  if (newValue) {
    // Enable notification features
  } else {
    // Disable notification features
  }
})
```

## Generic Event Handler

### on()

Generic event handler for any StreamType. Use specific methods above where possible.

```typescript theme={null}
on<T extends StreamType>(
  type: T,
  handler: (data: StreamDataTypes[T]) => void
): () => void
```

**Parameters:**

* `type`: The [`StreamType`](/app-devs/reference/enums#streamtype) to listen for
* `handler`: Callback function that receives data of the appropriate type for the specified StreamType

**Returns:** An unsubscribe function to remove the handler

**Example:**

```typescript theme={null}
import {StreamType} from "@mentra/sdk"

// Subscribe to a specific stream type
appSession.events.on(StreamType.LOCATION_UPDATE, data => {
  console.log(`Location update: ${data.lat}, ${data.lng}`)
})

// Using a language-specific stream
const transcriptionStream = createTranscriptionStream("en-US")
appSession.events.on(transcriptionStream, data => {
  console.log(`English transcription: ${data.text}`)
})
```
