DevExtreme v24.2 is now available.

Explore our newest features/capabilities and share your thoughts with us.

Your search did not match any results.

JavaScript/jQuery Chat - Overview

JavaScript Chat is an interactive UI component designed to send/receive messages in real time.

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

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

DevExtreme Accessibility Compliance
DevExtreme component libraries meet a variety of WCAG and Section 508 compliance standards. To assess this demo’s accessibility level, click the Run AXE® Validation button to launch the AXE® web accessibility evaluation tool.
All trademarks or registered trademarks are property of their respective owners. AXE® Terms of Use
The overall accessibility level of your application depends on the Chat features used.
Backend API
$(() => { function onMessageEntered({ message }) { userChat.renderMessage(message); supportChat.renderMessage(message); } function userChatTypingStart() { supportChat.option('typingUsers', [currentUser]); } function userChatTypingEnd() { supportChat.option('typingUsers', []); } function supportChatTypingStart() { userChat.option('typingUsers', [supportAgent]); } function supportChatTypingEnd() { userChat.option('typingUsers', []); } const userChat = $('#user-chat').dxChat({ items: initialMessages, user: currentUser, onMessageEntered, onTypingStart: userChatTypingStart, onTypingEnd: userChatTypingEnd, }).dxChat('instance'); const supportChat = $('#support-chat').dxChat({ items: initialMessages, user: supportAgent, onMessageEntered, onTypingStart: supportChatTypingStart, onTypingEnd: supportChatTypingEnd, }).dxChat('instance'); });
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script>window.jQuery || document.write(decodeURIComponent('%3Cscript src="js/jquery.min.js"%3E%3C/script%3E'))</script> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/24.2.7/css/dx.light.css" /> <script src="js/dx.all.js"></script> <script src="data.js"></script> <link rel="stylesheet" type="text/css" href="styles.css" /> <script src="index.js"></script> </head> <body class="dx-viewport"> <div class="demo-container"> <div id="user-chat"></div> <div id="support-chat"></div> </div> </body> </html>
.demo-container { display: flex; gap: 20px; } .dx-chat { height: 710px; } .dx-avatar { border: 1px solid var(--dx-color-border); }
const getTimestamp = function (date, offsetMinutes = 0) { return date.getTime() + offsetMinutes * 60000; }; const date = new Date(); date.setHours(0, 0, 0, 0); const currentUser = { id: 'c94c0e76-fb49-4b9b-8f07-9f93ed93b4f3', name: 'John Doe', }; const supportAgent = { id: 'd16d1a4c-5c67-4e20-b70e-2991c22747c3', name: 'Support Agent', avatarUrl: '../../../../images/petersmith.png', }; const initialMessages = [ { timestamp: getTimestamp(date, -9), author: supportAgent, text: 'Hello, John!\nHow can I assist you today?', }, { timestamp: getTimestamp(date, -7), author: currentUser, text: "Hi, I'm having trouble accessing my account.", }, { timestamp: getTimestamp(date, -7), author: currentUser, text: 'It says my password is incorrect.', }, { timestamp: getTimestamp(date, -7), author: supportAgent, text: 'I can help you with that. Can you please confirm your UserID for security purposes?', }, { timestamp: getTimestamp(date, 1), author: currentUser, text: 'john.doe1357', }, { timestamp: getTimestamp(date, 1), author: supportAgent, text: '✅ Instructions to restore access have been sent to the email address associated with your account.', }, ];

Messages

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

Use the following API to render new messages:

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 a 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 JavaScript Chat component 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 complete text entry, our JavaScript Chat component raises typingStart and typingEnd events. Use these events to manage the typingUsers array. The DevExtreme JavaScript Chat uses this array to display a list of active users.