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

# Events

You can listen for emitted events by subscribing to them using the [on](#on) method with your custom callback.
For example, your function can be executed every time a message is received.

## Availability updated

Indicates about a changed availability status.

```js theme={null}
customerSDK.on("availability_updated", ({ availability }) => {
  console.log("availability_updated", availability);
});
```

Payload:

| properties   | type   | description                                                    |
| ------------ | ------ | -------------------------------------------------------------- |
| availability | string | The availability status. Possible values: `online`, `offline`. |

## Chat deactivated

Indicates that the thread has been closed.

```js theme={null}
customerSDK.on("chat_deactivated", (payload) => {
  const { chatId } = payload;
  console.log("chat_deactivated", { chatId });
});
```

Payload:

| properties | type   | description  |
| ---------- | ------ | ------------ |
| chatId     | string | The chat ID. |

## Chat properties deleted

Indicates about deleted chat properties.

```js theme={null}
customerSDK.on("chat_properties_deleted", (payload) => {
  const { chatId, properties } = payload;
  console.log("chat_properties_deleted", { chatId, properties });
});
```

Payload:

| properties | type   | description          |
| ---------- | ------ | -------------------- |
| chatId     | string | The chat ID.         |
| properties | object | The chat properties. |

## Chat properties updated

Indicates about updated chat properties.

```js theme={null}
customerSDK.on("chat_properties_updated", (payload) => {
  const { chatId, properties } = payload;
  console.log("chat_properties_updated", { chatId, properties });
});
```

Payload:

| properties | type   | description          |
| ---------- | ------ | -------------------- |
| chatId     | string | The chat ID.         |
| properties | object | The chat properties. |

## Chat transferred

Indicates that a chat was transferred to a different group or an agent.

```js theme={null}
customerSDK.on("chat_transferred", (payload) => {
  const { chatId, threadId, transferredTo } = payload;
  console.log("chat_transferred", {
    chatId,
    threadId,
    transferredTo,
  });
});
```

Payload:

| properties             | type      | description                                                         |
| ---------------------- | --------- | ------------------------------------------------------------------- |
| chatId                 | string    | The chat ID.                                                        |
| threadId               | string    | The thread ID.                                                      |
| reason                 | string    | The reason for the transfer.                                        |
| requesterId            | string    | Returned only if `reason` is `manual`.                              |
| transferredTo          |           |                                                                     |
| transferredTo.agentIds | string\[] | Optional. The IDs of agents the chat was transferred to.            |
| transferredTo.groupIds | number\[] | Optional. The IDs of groups the chat was transferred to.            |
| queue                  |           | Optional.                                                           |
| queue.position         | number    | The current position in the queue.                                  |
| queue.waitTime         | number    | The estimated waiting time for an agent to be assigned, in seconds. |
| queue.queuedAt         | string    | The time at which the customer was queued, in RFC 3339 format.      |

## Connected

Indicates that the connection has been established.

```js theme={null}
customerSDK.on("connected", (payload) => {
  const { customer, availability, greeting } = payload;
  console.log("connected", { customer, availability, greeting });
});
```

Payload:

| argument     | type     | description                                                                   |
| ------------ | -------- | ----------------------------------------------------------------------------- |
| customer     | object   | The customer object. Same as the response from [`getCustomer`](#getcustomer). |
| availability | string   | The availability status. Possible values: `online`, `offline`.                |
| greeting     | greeting | Optional. A greeting received before the thread started.                      |

Greeting:

| argument           | type    | description                                                           |
| ------------------ | ------- | --------------------------------------------------------------------- |
| id                 | number  | The greeting template ID.                                             |
| text               | string  | The text content of the greeting.                                     |
| uniqueId           | string  | Unique event ID of the greeting.                                      |
| displayedFirstTime | boolean | If set to `true`: the greeting is being displayed for the first time. |
| accepted           | boolean | If set to `true`: the greeting was accepted by the customer.          |
| agent.id           | string  | The agent's ID.                                                       |
| agent.name         | string  | The agent's name.                                                     |
| agent.avatar       | string  | The agent's avatar URL.                                               |
| agent.jobTitle     | string  | The agent's job title.                                                |
| agent.isBot        | boolean | If set to `true`: the agent is a bot.                                 |

## Connection recovered

Indicates that the SDK has recovered from an "unstable" connection state. It's always preceded by the [`"connection_unstable"`](#connection-unstable) event.

```js theme={null}
customerSDK.on("connection_recovered", () => {
  console.log("connection_recovered");
});
```

This event doesn't carry any additional payload.

## Connection unstable

Indicates that the SDK has detected poor connection quality. It doesn't mean that it has disconnected from the server just yet.

```js theme={null}
customerSDK.on("connection_unstable", () => {
  console.log("connection_unstable");
});
```

This event doesn't carry any additional payload.

## Customer ID

Indicates about the ID of the customer.

```js theme={null}
customerSDK.on("customer_id", (id) => {
  console.log("customer id is", id);
});
```

Payload:

| argument | type   |
| -------- | ------ |
| id       | string |

## Customer page updated

The customer moved to another page, for example by following a link on your website.

```js theme={null}
customerSDK.on("customer_page_updated", (payload) => {
  const { url, title, openedAt } = payload;
  console.log("customer_page_updated", { url, title, openedAt });
});
```

Payload:

| properties | type   |                                                     |
| ---------- | ------ | --------------------------------------------------- |
| url        | string | The URL of the page the customer is currently on.   |
| title      | string | The title of the page the customer is currently on. |
| openedAt   | string | The date of the last update in RFC 3339 format.     |

## Customer updated

Indicates that customer's data was updated.
Each property in payload is available only if it was updated.

```js theme={null}
customerSDK.on("customer_updated", (customer) => {
  if (customer.name) {
    console.log(`Name got updated to: ${customer.name}`);
  }
  if (customer.email) {
    console.log(`Email got updated to: ${customer.email}`);
  }
  if (customer.avatar) {
    console.log(`Avatar got updated to: ${customer.avatar}`);
  }
  if (customer.fields) {
    console.log(`Fields got updated:`);
    console.log(customer.fields);
  }
});
```

Payload:

| properties | type   |
| ---------- | ------ |
| name       | string |
| email      | string |
| avatar     | string |
| fields     | object |

## Disconnected

Indicates that SDK has disconnected from the server. The event provides the disconnection reason.

```js theme={null}
customerSDK.on("disconnected", (payload) => {
  const { reason } = payload;
  console.log("disconnected", { reason });
});
```

Payload:

| properties | type   | description                                                                    |
| ---------- | ------ | ------------------------------------------------------------------------------ |
| reason     | string | The disconnection reason. See [disconnection reasons](#disconnection-reasons). |

## Event properties deleted

Indicates about the event properties that were deleted.

```js theme={null}
customerSDK.on("event_properties_deleted", (payload) => {
  const { chatId, threadId, eventId, properties } = payload;
  console.log("event_properties_deleted", {
    chatId,
    threadId,
    eventId,
    properties,
  });
});
```

Payload:

| properties | type   | description           |
| ---------- | ------ | --------------------- |
| chatId     | string | The chat ID.          |
| threadId   | string | The thread ID.        |
| eventId    | string | The event ID.         |
| properties | object | The event properties. |

## Event properties updated

Indicates about the event properties that were updated.

```js theme={null}
customerSDK.on("event_properties_updated", (payload) => {
  const { chatId, threadId, eventId, properties } = payload;
  console.log("event_properties_updated", {
    chatId,
    threadId,
    eventId,
    properties,
  });
});
```

Payload:

| properties | type   | description           |
| ---------- | ------ | --------------------- |
| chatId     | string | The chat ID.          |
| threadId   | string | The thread ID.        |
| eventId    | string | The event ID.         |
| properties | object | The event properties. |

## Event updated

Indicates that an event was updated.

```js theme={null}
customerSDK.on("event_updated", (payload) => {
  const { chatId, threadId, event } = payload;
  console.log("event_updated", { chatId, threadId, event });
});
```

Payload:

| properties | type   | description               |
| ---------- | ------ | ------------------------- |
| chatId     | string | The chat ID.              |
| threadId   | string | The thread ID.            |
| event      | object | The updated event object. |

## Events marked as seen

Indicates that the events were seen by the particular user.

```js theme={null}
customerSDK.on("events_marked_as_seen", (payload) => {
  const { chatId, userId, seenUpTo } = payload;
  console.log("events_marked_as_seen", { chatId, userId, seenUpTo });
});
```

Payload:

| properties | type   | description                                                              |
| ---------- | ------ | ------------------------------------------------------------------------ |
| chatId     | string | The chat ID.                                                             |
| userId     | string | The user ID.                                                             |
| seenUpTo   | string | The timestamp up to which events are marked as seen, in RFC 3339 format. |

## Greeting accepted

Indicates about a greeting accepted by the customer.

```js theme={null}
customerSDK.on("greeting_accepted", (payload) => {
  console.log("greeting_accepted", payload.uniqueId);
});
```

Payload:

| properties | type   |
| ---------- | ------ |
| uniqueId   | string |

## Greeting canceled

Indicates about a greeting canceled by the customer. It is also emitted when a new greeting automatically cancels the currently displayed one.

```js theme={null}
customerSDK.on("greeting_canceled", (payload) => {
  console.log("greeting_canceled", payload.uniqueId);
});
```

Payload:

| properties | type   |
| ---------- | ------ |
| uniqueId   | string |

## Incoming chat

Indicates about a newly started chat thread.
The payload contains the chat data structure and an object describing the new thread.
If the chat was started with some initial events, they will be included in the thread object.

```js theme={null}
customerSDK.on("incoming_chat", (payload) => {
  const { chat } = payload;
  const { id, access, users, properties, thread } = chat;
  console.log("incoming_chat", { id, access, users, properties, thread });
});
```

Payload:

| properties      | type      | description                                    |
| --------------- | --------- | ---------------------------------------------- |
| chat.id         | string    | The chat ID.                                   |
| chat.access     | object    | The initial access configuration for the chat. |
| chat.users      | object\[] | The users participating in the chat.           |
| chat.properties | object    | The chat properties.                           |
| chat.thread     | object    | The initial thread object.                     |

## Incoming event

Indicates about an incoming event sent to a chat.
You should distinguish received events by their types.

```js theme={null}
customerSDK.on("incoming_event", (payload) => {
  const { chat, event } = payload;
  switch (event.type) {
    case "message":
      console.log("new message - ", event.text);
      break;
    default:
      break;
  }
});
```

Payload:

| properties | type   | description      |
| ---------- | ------ | ---------------- |
| type       | string | The event type.  |
| ...        |        | Other properties |

## Incoming greeting

Indicates about an incoming greeting.

```js theme={null}
customerSDK.on("incoming_greeting", (payload) => {
  const { text, agent } = payload;
  const { name } = agent;
  console.log(`Received a greeting with "${text}" text content from ${name}.`);
});
```

Payload:

| properties         | type    | description                                                           |
| ------------------ | ------- | --------------------------------------------------------------------- |
| id                 | number  | The greeting template ID.                                             |
| text               | string  | The text content of the greeting.                                     |
| uniqueId           | string  | Unique ID of the greeting.                                            |
| displayedFirstTime | boolean | If set to `true`: the greeting is being displayed for the first time. |
| accepted           | boolean | If set to `true`: the greeting was accepted by the customer.          |
| agent              | object  | The agent who sent the greeting.                                      |

## Incoming rich message postback

Indicates about an incoming rich message postback.

```js theme={null}
customerSDK.on("incoming_rich_message_postback", (payload) => {
  const { chatId, threadId, eventId, userId, postback } = payload;
  console.log("incoming_rich_message_postback", {
    chatId,
    threadId,
    eventId,
    userId,
    postback,
  });
});
```

Payload:

| properties       | type    | description                                           |
| ---------------- | ------- | ----------------------------------------------------- |
| chatId           | string  | The ID of the chat containing the postback.           |
| threadId         | string  | The ID of the thread containing the postback.         |
| eventId          | string  | The ID of the event containing the postback.          |
| userId           | number  | The ID of the user who sent the postback.             |
| postback.id      | boolean | The postback ID.                                      |
| postback.toggled | boolean | If set to `true`: the postback is in a toggled state. |

## Incoming typing indicator

Indicates that one of the chat users is currently typing a message.
The message hasn't been sent yet.
The payload contains the typing indicator object.

```js theme={null}
customerSDK.on("incoming_typing_indicator", (payload) => {
  if (payload.typingIndicator.isTyping) {
    console.log(
      `user with ${payload.typingIndicator.authorId} id is writing something in ${payload.chatId}`,
    );
  } else {
    console.log(
      `user with ${payload.typingIndicator.authorId} id stopped writing in ${payload.chatId}`,
    );
  }
});
```

Payload:

| properties               | type    | description                                     |
| ------------------------ | ------- | ----------------------------------------------- |
| chatId                   | string  | Chat ID                                         |
| typingIndicator          |         |                                                 |
| typingIndicator.authorId | string  | User ID                                         |
| typingIndicator.isTyping | boolean | If set to `true`: the user is currently typing. |

## Queue position updated

Indicates that the queue position has been updated.

```js theme={null}
customerSDK.on("queue_position_updated", (payload) => {
  console.log(payload.chatId);
  console.log(payload.threadId);
  console.log(payload.queue.position);
  console.log(payload.queue.waitTime);
});
```

Payload:

| properties     | type   | description                                                         |
| -------------- | ------ | ------------------------------------------------------------------- |
| chatId         | string | The chat ID.                                                        |
| threadId       | string | The thread ID.                                                      |
| queue          |        |                                                                     |
| queue.position | number | The current position in the queue.                                  |
| queue.waitTime | number | The estimated waiting time for an agent to be assigned, in seconds. |

## Thread properties deleted

Indicates about deleted thread properties.

```js theme={null}
customerSDK.on("thread_properties_deleted", (payload) => {
  const { chatId, threadId, properties } = payload;
  console.log("thread_properties_deleted", { chatId, threadId, properties });
});
```

Payload:

| properties | type   | description            |
| ---------- | ------ | ---------------------- |
| chatId     | string | The chat ID.           |
| threadId   | string | The thread ID.         |
| properties | object | The thread properties. |

## Thread properties updated

Indicates about updated thread properties.

```js theme={null}
customerSDK.on("thread_properties_updated", (payload) => {
  const { chatId, threadId, properties } = payload;
  console.log("thread_properties_updated", { chatId, threadId, properties });
});
```

Payload:

| properties | type   | description            |
| ---------- | ------ | ---------------------- |
| chatId     | string | The chat ID.           |
| threadId   | string | The thread ID.         |
| properties | object | The thread properties. |

## User added to chat

Indicates that a user was added to a chat.

```js theme={null}
customerSDK.on("user_added_to_chat", (payload) => {
  const { chatId, user, present } = payload;
  console.log("user_added_to_chat", { chatId, user, present });
});
```

Payload:

| properties | type    | description                                                  |
| ---------- | ------- | ------------------------------------------------------------ |
| chatId     | string  | The chat ID.                                                 |
| user       | object  | The user added to the chat.                                  |
| present    | boolean | If set to `true`: the user is currently present in the chat. |

## User data

Contains the information about the User data.

```js theme={null}
customerSDK.on("user_data", (user) => {
  console.log(user);
});
```

### Customer user type

| properties | type           | description |
| ---------- | -------------- | ----------- |
| type       | 'customer'     |             |
| id         | string         |             |
| name       | string         | Optional    |
| email      | string         | Optional    |
| avatar     | string         | Optional    |
| fields     | customerFields |             |

### Agent user type

| properties | type    |
| ---------- | ------- |
| type       | 'agent' |
| id         | string  |
| name       | string  |
| avatar     | string  |
| jobTitle   | string  |

## User removed from chat

Indicates that a user was removed from a chat.

```js theme={null}
customerSDK.on("user_removed_from_chat", (payload) => {
  const { chatId, userId } = payload;
  console.log("user_removed_from_chat", { chatId, userId });
});
```

Payload:

| properties | type   | description  |
| ---------- | ------ | ------------ |
| chatId     | string | The chat ID. |
| userId     | string | The user ID. |
