# Events & Message Handling

## Events

### State Changes

```ts
client.on('stateChange', (state) => {
  console.log(state.agentState, state.isConnected);
});
```

### New Messages

```ts
client.on('message', (message) => {
  console.log('Message:', message.type, message.content);
});
```

### Messages Updated

```ts
client.on('messagesChange', (messages) => {
  console.log('Total messages:', messages.length);
});
```

### Real-time Transcription

```ts
client.on('userTranscriptionChange', (text) => {
  console.log('You said:', text);
});
```

### Lifecycle Events

```ts
client.on('connect', () => console.log('Connected'));
client.on('disconnect', () => console.log('Disconnected'));
client.on('botReady', () => console.log('Bot is ready'));
```

### Errors

```ts
client.on('error', (err) => {
  console.error('Convai error:', err);
});
```

***

## Message Types

Convai messages include:

* `user-transcription`
* `bot-llm-text`
* `bot-emotion`
* `action`
* `behavior-tree`

Only some are shown in UIs (usually transcription + bot text).

***

## Removing Listeners

```ts
const unsub = client.on('message', handler);
unsub(); // Remove listener
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.convai.com/api-docs/plugins-and-integrations/web-plugins/convai-web-sdk/vanilla-typescript/events-and-message-handling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
