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

# Events

> React to the chat from your own code: new replies, unread count, conversation started, chat opened and closed.

## Listen to an event

```js Website theme={null}
$talqui.on("messageReceived", function (event) {
  console.log(event.operatorName, event.preview);
});
```

```tsx React Native theme={null}
useTalquiEvent('messageReceived', (event) => {
  console.log(event.operatorName, event.preview);
});
```

```dart Flutter theme={null}
talqui.events
    .where((event) => event.name == 'messageReceived')
    .listen((event) => print(event.payload['preview']));
```

## All events

| Event | When | Payload | Website | React Native · Flutter |
| - | - | - | :-: | :-: |
| `ready` | The chat finished loading | `{ webchatVersion }` on the website; `{ protocol, webchatVersion, identity }` in the apps | ✓ | ✓ |
| `sessionStarted` | A conversation started, or an open one was restored | `{ sessionId }` (may be `null` right after the first message) | ✓ | ✓ |
| `messageSent` | The user sent a message | `{ messageId, type }` | ✓ | ✓ |
| `messageReceived` | Your team, a bot or an AI agent replied | `{ messageId, type, preview, operatorName }` | ✓ | ✓ |
| `unreadChanged` | The unread count changed | `{ count }` | ✓ | ✓ |
| `error` | A command was rejected | `{ code, message }` | ✓ | ✓ |
| `chatOpened` / `chatClosed` | The chat window opened or closed | `{}` | ✓ | |
| `resetDone` | `reset()` finished | `{}` | ✓ | |
| `closeRequested` | The user tapped close in the chat header | `{}` | | ✓ |
| `openUrl` | The user tapped a link (already opened for you, see `onOpenUrl`) | `{ url }` | | ✓ |

* `type` is the message type: `text`, `image`, `audio`, `video`, `file`…
* `preview` is the first 140 characters of a text reply, and empty for media.
* `operatorName` is the first name of the person who replied, or `null` for bots and AI agents.
* `error.code` is one of `invalid_params`, `invalid_payload`, `unknown_command` or `connection_failed`.

## Unread count

A reply counts as unread while the chat isn't on screen: closed, in a tab that isn't focused, or (on the website) in a browser tab in the background. Showing the chat resets the count to 0.

```js Website theme={null}
$talqui.on("unreadChanged", function (event) {
  document.title = event.count > 0 ? "(" + event.count + ") My shop" : "My shop";
});
```

```tsx React Native theme={null}
const { unreadCount } = useTalqui();
return <Badge count={unreadCount} />;
```

```dart Flutter theme={null}
ValueListenableBuilder<int>(
  valueListenable: talqui.unreadCount,
  builder: (context, unread, _) => Badge(isLabelVisible: unread > 0, label: Text('$unread')),
);
```

In the apps, replies are only counted once the chat has loaded. Call `preload()` after login so the badge works before the user first opens the chat.

## Examples

### Send replies to your analytics

```js theme={null}
$talqui.on("messageSent", function (event) {
  analytics.track("support_message_sent", { type: event.type });
});
```

### Close your own container when the user taps close

When you show `TalquiChat` / `<TalquiChat>` inside your own modal or sheet, close it on `closeRequested`:

```tsx React Native theme={null}
<TalquiChat style={{ flex: 1 }} onCloseRequested={() => setSheetOpen(false)} />
```

```dart Flutter theme={null}
talqui.events
    .where((event) => event.name == 'closeRequested')
    .listen((_) => Navigator.of(context).pop());
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.