Drive jqxChat from code and watch its events in a live log.
A live JavaScript/jQuery example of jqxChat, the jQWidgets Chat component, with its complete source code below. It is one of 5 jqxChat demos for jQuery.
colorScheme property – Sets or gets the colour scheme of the chat.height property – Sets or gets the chat's height.sendRequest property – Sets or gets a callback that fully controls transport: function(payload, widget) .theme property – Sets the widget's theme.title property – Sets or gets the header title text (also used as the popup launcher tooltip).welcomeMessage property – Sets or gets the welcome text (Markdown supported) shown in the empty state above the starter prompts.width property – Sets or gets the chat's width.sendMessage method – Sends a message to the assistant.addMessage method – Appends a message to the conversation and the view without contacting the provider.getMessages method – Returns a copy of the conversation history as an array of message objects.clearConversation method – Clears the conversation and restores the empty state (welcome message and starter prompts).To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxbuttons.js, jqxchat.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers), mockassistant.js.
$(document).ready(function () {
var theme = 'light';
$('#chat').jqxChat({
colorScheme: 'auto',
theme: theme,
width: 420,
height: 560,
title: 'API Playground',
welcomeMessage: 'Use the buttons on the right to drive me from code.',
sendRequest: jqxChatMock
});
function log(name, args) {
var detail = '';
if (args && args.content) { detail = ': ' + args.content.slice(0, 60); }
else if (args && args.message) { detail = ': ' + args.message; }
$('#log').prepend($('<div></div>').text(name + detail));
}
// every jqxChat event, logged live.
var events = ['messageSent', 'responseStart', 'responseDelta', 'messageReceived',
'responseEnd', 'stop', 'error'];
$.each(events, function (i, name) {
$('#chat').on(name, function (e) {
if (name === 'responseDelta') { return; } // too chatty to log each token
log(name, e.args);
});
});
// ---- method buttons ----
$('.demo-btn').jqxButton({ theme: theme, width: 190, height: 30 });
$('#askBtn').on('click', function () {
$('#chat').jqxChat('sendMessage', 'Show me a code example');
});
$('#addBtn').on('click', function () {
$('#chat').jqxChat('addMessage', 'assistant', 'This message was injected with **addMessage()** - it is not sent to any model.');
});
$('#stopBtn').on('click', function () {
$('#chat').jqxChat('stop');
});
$('#getBtn').on('click', function () {
var msgs = $('#chat').jqxChat('getMessages');
log('getMessages() -> ' + msgs.length + ' messages');
});
$('#clearBtn').on('click', function () {
$('#chat').jqxChat('clearConversation');
log('clearConversation()');
});
});
<div class="wrap">
<div id="chat"></div>
<div class="side">
<h3>Methods</h3>
<input type="button" class="demo-btn" id="askBtn" value="sendMessage('...')">
<input type="button" class="demo-btn" id="addBtn" value="addMessage('assistant', ...)">
<input type="button" class="demo-btn" id="stopBtn" value="stop()">
<input type="button" class="demo-btn" id="getBtn" value="getMessages()">
<input type="button" class="demo-btn" id="clearBtn" value="clearConversation()">
<h3 style="margin-top:14px;">Event log</h3>
<div id="log"></div>
</div>
</div>