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

# Add Event Callback Function

> How to use callback functions with events in VmonsterRoom.

In the context of the VmonsterRoom instance, the callback function plays a vital role in responding to various Event types. Here's what you need to know about using callback functions:

* **Event Handling**: The callback function is invoked when its associated
  Event takes place. This enables you to execute specific logic in response to
  changes or actions within the Room.
* **Single Listener Binding**: When attaching a callback function via the
  `on()` method, remember that only one listener can be attached per event type.
  If a new callback is registered for the same event, it will replace the
  previous listener.
* **Function Parameters**: Depending on the event type, the callback function
  may receive parameters that provide context or data related to the event.
  Tailor your callback to handle this data appropriately.
* **Integration with Room**: Utilizing the callback function allows you to
  seamlessly integrate interactive features within the VmonsterRoom instance,
  enhancing the responsiveness of your application to User and AI Avatar actions.

This approach ensures that your application remains dynamic and responsive, adapting to real-time events as they occur within the VmonsterRoom.

***

## on()

The `on()` method is used to attach an event listener to a Event.

```ts theme={null}
on(event: RoomEvent, callback: Function): void
```

You can only register one listener per event type. This means that if you register a new listener for an event, it will overwrite any previously registered listener for that event.

The callback function you provide will be executed when the specified event occurs. It should be tailored to handle the corresponding event type effectively.

| event                     | callback function type    |
| ------------------------- | ------------------------- |
| `joined`                  | () ⇒ void                 |
| `joining`                 | () ⇒ void                 |
| `left`                    | () ⇒ void                 |
| `aiavatar-start-speaking` | () ⇒ void                 |
| `aiavatar-message`        | (message: string) ⇒ void  |
| `aiavatar-stop-speaking`  | () ⇒ void                 |
| `stt-data`                | (sttData: STTData) ⇒ void |
| `stt-error`               | (error: any) ⇒ void       |
