Live Chat SDK
This guide explains how to install the Relay Pilot chat widget and control its behaviour using the relaychat() JavaScript API.
Overview
Once installed, the Relay Pilot widget exposes a global function:
relaychat()You can use this function to:
Open or close the chat window
Hide or show the widget
Pass visitor information
Listen for events (messages, unread count, open/close, etc.)
Fully remove the widget from the page
Basic Installation
Add the following embed code to your website, ideally just before the closing </body> tag.
<script>
(function(w,d,s,f,js,fjs){
w.relaychat = w.relaychat || function(){
(w.relaychat.q = w.relaychat.q || []).push(arguments)
};
js = d.createElement(s);
fjs = d.getElementsByTagName(s)[0];
js.id = 'relay-chat';
js.src = f;
js.async = 1;
fjs.parentNode.insertBefore(js, fjs);
})(window, document, 'script', 'https://app.relaypilot.io/widget/relay-chat.js');
relaychat('init', {
widgetId: 'YOUR_WIDGET_ID'
});
</script>Replace YOUR_WIDGET_ID with the Widget ID found in your Relay Pilot dashboard.
Opening & Closing the Widget
relaychat('open'); // Open the chat window
relaychat('close'); // Close the chat window
relaychat('toggle'); // Toggle open / closedExample: Custom Chat Button
<button onclick="relaychat('open')">
Chat with us
</button>Hiding & Showing the Widget
Hide or show the widget without destroying it:
relaychat('hide'); // Hide widget completely
relaychat('show'); // Show widget againExample: Hide Chat on Checkout Page
if (window.location.pathname === '/checkout') {
relaychat('hide');
}Removing the Widget Entirely
Completely removes the widget from the page and memory.
relaychat('shutdown');
// or
relaychat('destroy');Use this only if you do not plan to re-enable the widget.
Passing Visitor Information
During Initial Load
relaychat('boot', {
widgetId: 'YOUR_WIDGET_ID',
visitor: {
name: 'John Smith',
email: 'john@example.com'
}
});Updating After Load
relaychat('update', {
visitor: {
name: 'Jane Doe',
email: 'jane@example.com'
}
});This is useful when a user logs in or updates their profile.
Checking Widget State
relaychat('isOpen'); // true / false
relaychat('isReady'); // true / false
relaychat('getState'); // Full state objectExample response:
{
isOpen: false,
isReady: true,
isHidden: false,
isOnline: true,
unreadCount: 2,
conversationId: 'abc-123',
view: 'home'
}Events
Subscribe to widget events using:
relaychat('on', EVENT_NAME, callback);Available Events
Widget Ready
relaychat('on', 'ready', function(state) {
console.log('Chat widget is ready');
});Chat Opened
relaychat('on', 'open', function() {
console.log('Chat opened');
});Chat Closed
relaychat('on', 'close', function() {
console.log('Chat closed');
});New Message
relaychat('on', 'message', function(message) {
console.log('New message:', message.body);
});Unread Count Changed
relaychat('on', 'unreadCount', function(data) {
console.log('Unread messages:', data.count);
});Removing Event Listeners
function myHandler(data) {
// ...
}
relaychat('on', 'message', myHandler);
relaychat('off', 'message', myHandler);Common Implementation Examples
Auto-Open Chat on Contact Page
relaychat('on', 'ready', function() {
if (window.location.pathname === '/contact') {
relaychat('open');
}
});Show Chat Only After Login
relaychat('init', { widgetId: 'YOUR_WIDGET_ID' });
relaychat('hide');
function onLogin(user) {
relaychat('update', {
visitor: {
name: user.name,
email: user.email
}
});
relaychat('show');
}Custom Unread Badge
relaychat('on', 'unreadCount', function(data) {
document.getElementById('my-badge').textContent =
data.count > 0 ? data.count : '';
});React Example
useEffect(() => {
const handleUnread = (data) => setUnreadCount(data.count);
window.relaychat('on', 'unreadCount', handleUnread);
return () => {
window.relaychat('off', 'unreadCount', handleUnread);
};
}, []);Troubleshooting
Ensure the embed script loads before calling
relaychat()Confirm your Widget ID is correct
Check browser console for JavaScript errors
Verify no ad blockers or CSP rules are blocking the widget script
Last updated Jan 27, 2026