# Conversation (Chat)

A conversation(chat) object lets you have conversations with 1 or more [Users](/reference/user.md).&#x20;

You can view how a chat object can be created for a [one-to-one conversation](/javascript-sdk/overview.md#start-one-to-one-conversation) or a [group chat](/javascript-sdk/overview.md#start-group-chat) with multiple members.

## Event Listeners

### Listen for new messages <a href="#listen-for-messages" id="listen-for-messages"></a>

This is called whenever the current user receives a new message.

```javascript
chat.onMessage((message) => {

        //do something with message
        
})
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="149">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>message</code></td><td><a href="/pages/kHDmp1Ige5MDBB56e34a"><code>Message</code></a></td><td>the message that has been received.</td></tr></tbody></table>

### Listen for message updated <a href="#listen-for-messages" id="listen-for-messages"></a>

This is called whenever a message gets updated in the chat.

```javascript
chat.onMessageUpdated((message) => {

        //do something with the updated message
        
})
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="149">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>message</code></td><td><a href="/pages/kHDmp1Ige5MDBB56e34a"><code>Message</code></a></td><td>the message that has been updated.</td></tr></tbody></table>

### Listen for message deleted <a href="#listen-for-messages" id="listen-for-messages"></a>

This is called whenever a message gets deleted.

```javascript
chat.onMessageDeleted((messageId) => {

        //do something 
        
})
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="149">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>messageId</code></td><td><code>String</code></td><td>the id of the message that has been deleted.</td></tr></tbody></table>

### Listen for typing started <a href="#listen-for-messages" id="listen-for-messages"></a>

This is called whenever a participant of the conversation starts typing.

```jsx
chat.onTypingStarted((user) => {
       //do something when a user starts typing
 })
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="149">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>user</code></td><td><a href="/pages/aPDOW0ZeC8SCmThYhzfg"><code>User</code></a></td><td>the user that started typing</td></tr></tbody></table>

### Listen for typing stopped <a href="#listen-for-messages" id="listen-for-messages"></a>

This is called whenever a participant of the conversation stops typing.

```jsx
chat.onTypingStopped((user) => {
       //do something when a user stops typing
 })
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="149">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>user</code></td><td><a href="/pages/aPDOW0ZeC8SCmThYhzfg"><code>User</code></a></td><td>the user that stopped typing</td></tr></tbody></table>

### Listen for message seen events <a href="#listen-for-messages" id="listen-for-messages"></a>

This is called whenever a message has been seen by a participant in a conversation

```javascript
chat.onMessageSeen((messageId) => {
       //do something when a message is seen
 })
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="149">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>messageId</code></td><td><code>string</code></td><td>the id of the message that has been seen</td></tr></tbody></table>

### Listen for user status changes (online/offline)

This is called whenever the status of a user in a conversation changes from online to offline or vice versa.

```javascript
chat.onMemberStatusChanged((memberId,status) => {
       //do something when a user status changes
 })
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="109">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>memberId</code></td><td><code>string</code></td><td>the id of the user who's status has changed</td></tr><tr><td>status</td><td><code>enum</code> </td><td>the status of the user. an enum of either <code>ONLINE</code> or <code>OFFLINE</code></td></tr></tbody></table>

### Listen for AI Actions <a href="#listen-for-ai-actions" id="listen-for-ai-actions"></a>

Is triggered when an action configured on an AI agent is executed.

```javascript
chat.onAiAction(({event, chatbotUsername}) => {
    //do something with
})
```

<table><thead><tr><th width="190.33333333333331">Field</th><th width="105">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>event</code></td><td><code>string</code></td><td>The event name, this is defined on the minchat dashboard when creating/updating an AI Agent</td></tr><tr><td><code>chatbotUsername</code></td><td><code>string</code></td><td>the username of the AI agent that triggered the event</td></tr></tbody></table>

## Functions

### Get Messages

Messages retrieved in a conversation are paginated to 25 messages at a time.

{% tabs %}
{% tab title="async/await" %}

```javascript

const { messages , page , totalMessages , totalPages } = await chat.getMessages(1 /** page to query **/ )

```

{% endtab %}

{% tab title="callback" %}

```javascript
chat.getMessages(1 /** page to query **/)
  .then(({  messages , page , totalMessages , totalPages }) => {
       
         //do something with the messages  
         
  })
```

{% endtab %}
{% endtabs %}

**Parameters**

<table><thead><tr><th width="103">Field</th><th width="104">Type</th><th width="98">State</th><th>Description</th></tr></thead><tbody><tr><td><code>page</code></td><td><code>number</code></td><td><em>optional</em></td><td>query a specific page of messages, each page contains 25 messages. if <code>null</code> or <code>undefined</code> then the most recent 25 messages are returned</td></tr></tbody></table>

**Response**

<table><thead><tr><th width="192.33333333333331">Field</th><th width="149">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>messages</code></td><td><a href="/pages/kHDmp1Ige5MDBB56e34a"><code>Message[]</code></a></td><td>array of messages</td></tr><tr><td><code>page</code></td><td><code>number</code></td><td>shows the current page of messages. each page contains 25 messages</td></tr><tr><td><code>totalMessages</code></td><td><code>number</code></td><td>shows the total number of messages that exist</td></tr><tr><td><code>totalPages</code></td><td><code>number</code></td><td>shows the total number of pages of messages that exist. each page contains 25 messages</td></tr></tbody></table>

### Send Message

To send a message, call a function on the chat object.

<pre class="language-javascript"><code class="lang-javascript"><strong>const message = {
</strong>    text: "Hello World!"
<strong>}
</strong>
const callback = (data)=> { /**    
        do something
  */}

<strong>chat.sendMessage(message, callback)  
</strong></code></pre>

**`Message` Parameters**

<table><thead><tr><th width="140">Field</th><th width="149">Type</th><th width="98">State</th><th>Description</th></tr></thead><tbody><tr><td><code>text</code></td><td><code>string</code></td><td><em>optional</em></td><td>the text of the message</td></tr><tr><td><code>file</code></td><td><code>File</code></td><td><em>optional</em></td><td>upload a file as an attachment</td></tr><tr><td><code>metadata</code></td><td><code>json object</code></td><td>optional</td><td>an optional key value pair for any additional message information such as custom font size, font type, or <code>JSON</code> formatted string. accepts string,number and boolean values</td></tr></tbody></table>

**Callback**

<table><thead><tr><th width="125">Field</th><th width="133">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>data</code></td><td><a href="/pages/kHDmp1Ige5MDBB56e34a"><code>Message</code></a></td><td>the message that was sent</td></tr></tbody></table>

### Update Message

<pre class="language-javascript"><code class="lang-javascript">const message = {
    text: "Updated Hello World!"
<strong>}
</strong>
const callback = (data)=> { /**    
     do something
  */}

<strong>chat.updateMessage(messageId, message, callback)  
</strong></code></pre>

**Parameters**

<table><thead><tr><th width="148">Field</th><th width="149">Type</th><th width="98">State</th><th>Description</th></tr></thead><tbody><tr><td><code>messageId</code></td><td><code>string</code></td><td><em>required</em></td><td>the id of the message to be updated</td></tr></tbody></table>

**`Message` Parameters**

<table><thead><tr><th width="140">Field</th><th width="149">Type</th><th width="98">State</th><th>Description</th></tr></thead><tbody><tr><td><code>text</code></td><td><code>string</code></td><td><em>optional</em></td><td>the text of the message</td></tr><tr><td><code>file</code></td><td><code>File</code></td><td><em>optional</em></td><td>upload a file as an attachment</td></tr><tr><td><code>metadata</code></td><td><code>json object</code></td><td>optional</td><td>an optional key value pair for any additional message information such as custom font size, font type, or <code>JSON</code> formatted string. accepts string,number and boolean values</td></tr></tbody></table>

**Callback**

<table><thead><tr><th width="125">Field</th><th width="133">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>data</code></td><td><a href="/pages/kHDmp1Ige5MDBB56e34a"><code>Message</code></a></td><td>the message that was updated</td></tr></tbody></table>

### Delete Message

<pre class="language-javascript"><code class="lang-javascript"><strong>chat.deleteMessage(messageId)  
</strong></code></pre>

**Parameters**

<table><thead><tr><th width="151">Field</th><th width="107">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>messageId</code></td><td><code>string</code></td><td>the Id of the message to delete</td></tr></tbody></table>

### Get ID

Get the id of the conversation, returns a `string`

```javascript
const id = chat.getId()
```

### Get Title

Get the title of the conversation, returns a `string`

```javascript
const title = chat.getTitle()
```

### Get Conversation Avatar

Get the avatar of the chat, returns a `string`  which is the url to the avatar

```javascript
const avatar = chat.getChatAvatar()
```

### Get recent message

Get the most recent message sent in the conversation. Returns a [`Message`](/reference/message.md) object.

```javascript
const message = chat.getLastMessage()
```

### Get Members

Get an array of all the [users](/reference/user.md) of this conversation

```javascript
const users = await chat.getMembers()
```

### Get MemberIds

Get an array of ids of all the members of this conversation

```javascript
const userIds = await chat.getMemberIds()
```

### Get Metadata

get an optional key value pair for any additional chat information.

```javascript
const metadata = chat.getMetadata()
```

### Set Metadata

Add any additional chat information.

```javascript
await chat.setMetaData(metadata)
```

**Parameters**

<table><thead><tr><th width="151">Field</th><th width="148">Type</th><th width="98">State</th><th>Description</th></tr></thead><tbody><tr><td><code>metadata</code></td><td><code>json object</code></td><td><em>required</em></td><td>an optional key value pair for any additional chat information. Accepts string, number and boolean values</td></tr></tbody></table>

### Add member to group chat

Add a member to a group chat

```typescript
await chat.addMember(username)
```

### Add member to group chat by ID

add a member to a group chat using a member ID

```typescript
await chat.addMemberById(userId)
```

### Remove member from group chat

Remove a member from a group chat

<pre class="language-typescript"><code class="lang-typescript"><strong>await chat.removeMember(username)
</strong></code></pre>

### Remove member from group chat by ID

Remove a member from a group chat using a member ID

<pre class="language-typescript"><code class="lang-typescript"><strong>await chat.removeMemberById(userId)
</strong></code></pre>

### Notify everyone typing started

Call this function to notify everyone who is a participant of the conversation that the current user has started typing

```javascript
chat.startTyping()
```

### Notify everyone typing stopped

Call this function to notify everyone who is a participant of the conversation that the current user has stopped typing

```javascript
chat.stopTyping()
```

### Set Message Seen

Call this function to notify participants of the conversation that a message has been seen. (useful when handling message seen states).&#x20;

```javascript
chat.setSeen(messageId)
```

**Parameters**

<table><thead><tr><th width="151">Field</th><th width="113">Type</th><th width="98">State</th><th>Description</th></tr></thead><tbody><tr><td><code>messageId</code></td><td><code>string</code> </td><td><em>optional</em></td><td>the id of the message. If left null or undefined then the last message in the conversation is set as seen by the connected user.</td></tr></tbody></table>


---

# 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.minchat.io/javascript-sdk/conversation-chat.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.
