Your search did not match any results.

Chat - Overview

Chat is an interactive interface that allows users to send and receive messages in real time.

To get started with the DevExtreme Chat component, refer to the following step-by-step tutorial: Getting Started with Chat.

The demo implements basic Chat functionality: specifies initial messages, updates the conversation with new incoming and outgoing messages, manages users, and links two chats in real-time.

Backend API
@model DevExtreme.NETCore.Demos.ViewModels.ChatViewModel @(Html.DevExtreme().Chat() .ID("user-chat") .User(user => user .Id(Model.CurrentUser.Id) .Name(Model.CurrentUser.Name) ) .DataSource(Model.Messages) .ReloadOnChange(false) .OnMessageEntered("onMessageEntered") .OnTypingStart("userChatTypingStart") .OnTypingEnd("userChatTypingEnd") .OnInitialized("userChatInitialized") ) @(Html.DevExtreme().Chat() .ID("support-chat") .User(user => user .Id(Model.SupportAgent.Id) .Name(Model.SupportAgent.Name) .AvatarUrl(Model.SupportAgent.AvatarUrl) ) .DataSource(Model.Messages) .ReloadOnChange(false) .OnMessageEntered("onMessageEntered") .OnTypingStart("supportChatTypingStart") .OnTypingEnd("supportChatTypingEnd") .OnInitialized("supportChatInitialized") ) <script> var userChat, supportChat; function userChatInitialized({ component }) { userChat = component; } function supportChatInitialized({ component }) { supportChat = component; } function onMessageEntered({ message }) { userChat.renderMessage(message); supportChat.renderMessage(message); } function userChatTypingStart({ user }) { supportChat.option('typingUsers', [user]); } function userChatTypingEnd() { supportChat.option('typingUsers', []); } function supportChatTypingStart({ user }) { userChat.option('typingUsers', [user]); } function supportChatTypingEnd() { userChat.option('typingUsers', []); } </script>
using DevExtreme.NETCore.Demos.Models.SampleData; using DevExtreme.NETCore.Demos.ViewModels; using Microsoft.AspNetCore.Mvc; namespace DevExtreme.NETCore.Demos.Controllers { public class ChatController : Controller { public ActionResult Overview() { return View(new ChatViewModel { CurrentUser = SampleData.CurrentUser, SupportAgent = SampleData.SupportAgent, Messages = SampleData.Messages, }); } } }
using DevExtreme.NETCore.Demos.Models.Chat; using System; using System.Collections.Generic; namespace DevExtreme.NETCore.Demos.Models.SampleData { public partial class SampleData { private static readonly DateTime todayDate = DateTime.Now.Date; private static DateTime GetTimestamp(DateTime date, int offsetMinutes = 0) { DateTime adjustedDate = date.AddMinutes(offsetMinutes); return adjustedDate; } public static ChatUser CurrentUser = new ChatUser { Id = "c94c0e76-fb49-4b9b-8f07-9f93ed93b4f3", Name = "John Doe" }; public static ChatUser SupportAgent = new ChatUser { Id = "d16d1a4c-5c67-4e20-b70e-2991c22747c3", Name = "Support Agent", AvatarUrl = "../../images/petersmith.png" }; public static readonly IEnumerable<Message> Messages = new[] { new Message { Timestamp = GetTimestamp(todayDate, -9), Author = SupportAgent, Text = "Hello, John!\nHow can I assist you today?", }, new Message { Timestamp = GetTimestamp(todayDate, -7), Author = CurrentUser, Text = "Hi, I'm having trouble accessing my account.", }, new Message { Timestamp = GetTimestamp(todayDate, -7), Author = CurrentUser, Text = "It says my password is incorrect." }, new Message { Timestamp = GetTimestamp(todayDate, -7), Author = SupportAgent, Text = "I can help you with that. Can you please confirm your UserID for security purposes?" }, new Message { Timestamp = GetTimestamp(todayDate, 1), Author = CurrentUser, Text = "john.doe1357" }, new Message { Timestamp = GetTimestamp(todayDate, 1), Author = SupportAgent, Text = "✅ Instructions to restore access have been sent to the email address associated with your account." } }; } }
using DevExtreme.NETCore.Demos.Models.Chat; using System.Collections.Generic; namespace DevExtreme.NETCore.Demos.ViewModels { public class ChatViewModel { public IEnumerable<Message> Messages { get; set; } public ChatUser CurrentUser { get; set; } public ChatUser SupportAgent { get; set; } } }
using System; using System.Text.Json.Serialization; namespace DevExtreme.NETCore.Demos.Models.Chat { public class Message { [JsonPropertyName("timestamp")] public DateTime Timestamp { get; set; } [JsonPropertyName("text")] public string Text { get; set; } [JsonPropertyName("author")] public ChatUser Author { get; set; } } }
using System.Text.Json.Serialization; namespace DevExtreme.NETCore.Demos.Models.Chat { public class ChatUser { [JsonPropertyName("id")] public string Id { get; set; } [JsonPropertyName("name")] public string Name { get; set; } [JsonPropertyName("avatarUrl")] public string AvatarUrl { get; set; } } }
.demo-container { display: flex; gap: 20px; } .dx-chat { height: 710px; } .dx-avatar { border: 1px solid var(--dx-color-border); }

Messages

To specify initial messages, you can either populate the items array (shown in this demo) or use a dataSource.

Use the following API to render new messages:

  • If you use items, update the array with the new message.
  • If you use a dataSource, implement load and insert operations.

Users

To specify the chat owner, set the user property. Owner messages align to the right (or left in RTL mode) and do not display the name or avatar.

Each message includes information about the sender (author): name, avatar, and alternative avatar text. If no avatar is set, the user's initials are displayed instead.

Events

If a user enters a message, the Chat raises the messageEntered event. Use the event handler to process the message. For example, you can display the message in the message feed and send the message to the server for storage.

When users start or finish typing, the Chat raises typingStart and typingEnd events. Use these events to manage the typingUsers array. The Chat uses this array to display a list of active users.