Methods

Event log

jQuery Chat: Events and Methods

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.

jqxChat API used in this demo

Source code

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.

script.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()');
    });
});

body.html

<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>

More jQuery Chat demos