Vue Chat - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

Chat is a UI component that allows users to send and receive messages in real time.

This tutorial shows how to add Chat to the page and configure the component's core settings.

Each section in this tutorial describes a single configuration step. You can also find the full source code in the following GitHub repository:

View on GitHub

Create a Chat

Add DevExtreme to your Vue application and use the following code to create a Chat component:

App.vue
  • <template>
  • <DxChat />
  • </template>
  •  
  • <script setup>
  • import 'devextreme/dist/css/dx.fluent.blue.light.css';
  • import { DxChat } from 'devextreme-vue/chat';
  • </script>

To limit the Chat size, use the width and height properties:

App.vue
  • <template>
  • <DxChat
  • width="400"
  • height="450"
  • />
  • </template>

Render Sent Messages

When a user enters a message in the Chat, the messageEntered event is raised. You can use this event handler to render the message you entered.

In Vue, the primary way to render a new message is updating your items array or a dataSource. In this tutorial, the items array is used:

App.vue
  • <template>
  • <DxChat ...
  • :items="messages"
  • @message-entered="onMessageEntered"
  • />
  • </template>
  •  
  • <script setup>
  • import { ref } from "vue";
  • // ...
  •  
  • const messages = ref([]);
  • const onMessageEntered = ({ message }) => {
  • messages.value = [...messages.value, message];
  • };
  • </script>

Manage Users

Assign chat users:

  • Specify the user's ID.
  • Specify the user's name. If the name is not specified, it becomes "Unknown User".
  • Define avatars with a URL and alt text. If the URL is not specified, the avatar becomes the user's name initials.
  • Assign a user to the user property to define the chat owner.
App.vue
  • <template>
  • <DxChat ...
  • :user="firstUser"
  • />
  • </template>
  •  
  • <script setup>
  • // ...
  • const firstUser = {
  • id: "1",
  • name: "User"
  • };
  •  
  • const secondUser = {
  • id: "2",
  • name: "Feedback Bot",
  • avatarUrl: "./images/Chat/bot.png"
  • };
  • </script>

Set Initial Messages

If you want to specify initial messages in the Chat, use the items array.

App.vue
  • <template>
  • <DxChat ...
  • :items="messages"
  • />
  • </template>
  •  
  • <script setup>
  • import { ref } from "vue";
  • // ...
  • const initialMessages = [{
  • timestamp: Date.now(),
  • author: secondUser,
  • text: "Hello! We'd love to hear your feedback. Please share your thoughts below!"
  • }];
  •  
  • const messages = ref(initialMessages);

Post-Processing

Use onMessageEntered to perform message post processing (like sending the message to the server for storage). This tutorial simulates sending a message to a backend:

  1. The Feedback Bot user is added to the typingUsers array to show them typing.
  2. After one second, the typingUsers array is emptied, and Chat displays the answer message.
  3. After that, an alert is displayed, and Chat is disabled.
App.vue
  • <template>
  • <DxChat ...
  • v-model:alerts="alerts"
  • v-model:disabled="disabled"
  • />
  • </template>
  •  
  • <script setup>
  • import { ref } from "vue";
  • // ...
  •  
  • const messages = ref([]);
  • const alerts = ref([]);
  • const disabled = ref(false);
  •  
  • const onMessageEntered = ({ message }) => {
  • // ...
  • typingUsers.value = [secondUser];
  • sendToBackend();
  • };
  •  
  • const sendToBackend = () => {
  • setTimeout(() => {
  • typingUsers.value = [];
  • messages.value = [
  • ...messages.value,
  • {
  • text: "Thanks for helping us improve!",
  • author: secondUser,
  • timestamp: Date.now(),
  • },
  • ];
  • alerts.value = [
  • ...alerts.value,
  • {
  • id: 1,
  • message: "Session expired",
  • },
  • ];
  • disabled.value = true;
  • }, 1000);
  • };
  • </script>