Angular Chat - Data Binding

You can bind data to Chat in two ways: with the items array or the dataSource entity.

NOTE
Do not specify the items property if you specified dataSource, and vice versa.

items

The items array contains all messages in the Chat. To display an initial message, add it to this array. Add subsequent messages to the array to render them in Chat.

app.component.html
app.component.ts
  • <dx-chat
  • [items]="messages"
  • (onMessageEntered)="onMessageEntered($event)"
  • ></dx-chat>
  • import { DxChatTypes } from "devextreme-angular/ui/chat";
  • // ...
  • export class AppComponent {
  • messages: DxChatTypes.Message[] = [
  • {
  • timestamp: Date.now(),
  • author: secondUser,
  • text: "Hello! I'm here to help you. How can I assist you today?",
  • }
  • ];
  • onMessageEntered({ message }) {
  • this.messages = [...this.messages, message];
  • }
  • }

dataSource

For a more flexible solution than an array, use the dataSource property. For instance, dataSource allows Chat to handle server-side data processing through Web API, a store, or a DataSource object.

When you pass a store to dataSource, the DataSource instance is created automatically inside the Chat.

NOTE
If you use a separate DataSource object, disable pagination to ensure Chat functions properly.

When you use dataSource, the reloadOnChange property is crucial:

  • When you send a message in a Chat (press the "Send" button), the Chat triggers the store's insert method and adds the message to the store.
  • If reloadOnChange is enabled (default), the dataSource reloads: clears all items and calls the load method to update itself. Chat automatically listens to dataSource changes, and updates the message feed with new messages.
  • Disable reloadOnChange to manage large numbers of messages, prevent additional load requests, and control message rendering timing.

The following code snippet includes:

  • A CustomStore.
  • A DataSource (pagination disabled).
  • Push API for inserting and updating messages.
  • reloadOnChange: false.
NOTE
This code snippet illustrates basic principles and does not implement custom editing. For a complete application example, refer to the following demo: AI and Chatbot Integration.
app.component.html
app.component.ts
  • <dx-chat
  • [dataSource]="dataSource"
  • [reloadOnChange]="false"
  • (onMessageEntered)="onMessageEntered($event)">
  • </dx-chat>
  • import DataSource from 'devextreme/data/data_source';
  • import CustomStore from 'devextreme/data/custom_store';
  • import { DxChatTypes } from "devextreme-angular/ui/chat";
  • // ...
  •  
  • export class AppComponent {
  • messages: DxChatTypes.Message[] = [{
  • timestamp: new Date(),
  • text: "Hello! I'm here to help you. How can I assist you today?"
  • }];
  •  
  • customStore: CustomStore = new CustomStore({
  • key: "id",
  • load: () => {
  • return new Promise((resolve) => {
  • setTimeout(() => {
  • resolve([...this.messages]);
  • }, 0);
  • });
  • },
  • insert: (message) => {
  • return new Promise((resolve) => {
  • setTimeout(() => {
  • this.messages.push(message);
  • resolve(message);
  • });
  • });
  • },
  • });
  •  
  • dataSource: DataSource = new DataSource({
  • store: this.customStore,
  • paginate: false,
  • });
  •  
  • editing = null;
  •  
  • uniqueIndex = this.messages.length + 1;
  •  
  • onMessageEntered(e: DxChatTypes.MessageEnteredEvent) {
  • if (this.editing) {
  • this.dataSource.store().push([{ type: "update", key: this.editing, data: { text: e.message.text } }]);
  • this.editing = null;
  • }
  • else {
  • this.dataSource.store().push([
  • { type: "insert", data: { id: this.uniqueIndex++, ...e.message } }
  • ]);
  • }
  • };
  • }