
The Tugi Widget exposes public API functions that allow clients to:
- Open and close the widget
- Check notification state
- Subscribe to notification changes in real-time
- Control default button visibility through configuration
Opens the widget dialog and clears any notification state.
// Open the widget
window.tugiWidget.openWidget();
Closes the widget dialog.
// Close the widget
window.tugiWidget.closeWidget();Returns whether the widget has been initialized.
// Check if widget is ready
if (window.tugiWidget.isInitialized()) {
// Safe to use widget functions
window.tugiWidget.openWidget();
}Returns a Promise that resolves when the widget is initialized. This is useful for waiting for the widget to be ready before interacting with it.
// Wait for widget to be ready
window.tugiWidget.onWidgetReady()
.then(() => {
// Widget is now ready and safe to use
window.tugiWidget.openWidget();
})
.catch((error) => {
console.error('Widget failed to initialize:', error);
});Async function that waits for the widget to be ready. This is a convenience wrapper around `onWidgetReady()`.
// Using async/await
async function setupWidget() {
try {
await window.tugiWidget.waitForWidgetReady();
// Widget is now ready
window.tugiWidget.openWidget();
} catch (error) {
console.error('Failed to wait for widget:', error);
}
}Returns the current WebSocket connection state.
// Check connection status
const isConnected = window.tugiWidget.getWebSocketConnectionState();
if (isConnected) {
console.log('Widget is connected and ready');
}Returns the current notification state (boolean).
// Check if there's a notification
const hasNotification = window.tugiWidget.hasNotification();
if (hasNotification) {
// Show notification indicator
showNotificationBadge();
}
Subscribes to notification state changes. Returns an unsubscribe function for cleanup.
// Subscribe to notification changes
const unsubscribe = window.tugiWidget.onNotificationChange((hasNotification) => {
if (hasNotification) {
// Show notification indicator
showNotificationBadge();
console.log('New message received!');
} else {
// Hide notification indicator
hideNotificationBadge();
console.log('No new messages');
}
});
// Later, cleanup the listener
unsubscribe();
The widget also dispatches a `tugi-widget-notification` CustomEvent for each notification change, so you can alternatively use standard DOM event listeners.
The widget dispatches standard DOM events for better web compatibility:
Dispatched when notification state changes.
document.addEventListener('tugi-widget-notification', (event) => {
const hasNotification = event.detail.hasNotification;
if (hasNotification) {
showNotificationBadge();
} else {
hideNotificationBadge();
}
});
Dispatched when the widget is opened.
document.addEventListener('tugi-widget-opened', () => {
console.log('Widget opened');
});Dispatched when the widget is closed.
document.addEventListener('tugi-widget-closed', () => {
console.log('Widget closed');
});Dispatched when the widget is ready and initialized.
document.addEventListener('tugi-widget-ready', () => {
console.log('Widget is ready!');
// Safe to interact with widget now
window.tugiWidget.openWidget();
});You can hide the default button when initializing the widget by setting `showDefaultFloatingButton: false` in the configuration:
window.tugiWidget.initialize({
jwtFn: () => Promise.resolve('your-jwt-token'),
tenantId: 'your-tenant-id',
brandId: 'your-brand-id',
brandName: 'Your Brand',
customize: {
showDefaultFloatingButton: false, // This will hide the default button
// ... other customization options
}
});
The API functions are designed to be safe and will not throw errors if called before the widget is initialized. However, it's recommended to ensure the widget is properly initialized before calling these functions.
// Safe way to call API functions
if (window.tugiWidget && window.tugiWidget.isInitialized()) {
window.tugiWidget.openWidget();
}
// Safe way to setup notification listener
if (window.tugiWidget && typeof window.tugiWidget.onNotificationChange === 'function') {
const unsubscribe = window.tugiWidget.onNotificationChange((hasNotification) => {
// Handle notification change
// Cleanup when done
unsubscribe();
});
}
onNotificationChange() or CustomEvents instead of polling - Both provide real-time updatesremoveEventListener() to prevent memory leaksisInitialized() before calling widget functionsgetWebSocketConnectionState() to ensure the widget is connectedshowDefaultFloatingButton: false if you plan to use custom triggers
The notification system automatically updates when new messages are received via WebSocket, providing real-time feedback to your users! 🚀
Last Updated: [2025-10-04]