Control chat sessions programmatically with startNewSession() and track session lifecycle events
Enter a UUID or generate one, then start a new session with that specific ID:
Tip: clearChat() ends the session but doesn't start a new one until the next message. startNewSession() immediately creates a new session.
| Method | Ends Session | Clears Messages | Starts New Session | Shows Welcome | Returns SessionId |
|---|---|---|---|---|---|
| clearChat() | Yes | Yes | No (on next msg) | No | No |
| startNewSession() | Yes | Yes | Yes (immediate) | Yes | Yes |
// Initialize widget
const widget = window.NexusChatWidget.init({
experienceId: "your-experience-id",
apiUrl: "undefined"
});
// Listen to session events
widget.on('sessionStart', (data) => {
console.log('🟢 Session started:', data.sessionId);
});
widget.on('sessionEnd', (data) => {
console.log('🔴 Session ended:', {
sessionId: data.sessionId,
duration: data.duration
});
});
widget.on('sessionClear', (data) => {
console.log('🟡 Session cleared:', {
sessionId: data.sessionId,
messageCount: data.messageCount
});
});
// Start new session with auto-generated UUID
const newSessionId = widget.startNewSession();
console.log('New session:', newSessionId);
// Or start with specific UUID
const customSessionId = widget.startNewSession(
"550e8400-e29b-41d4-a716-446655440000"
);Session events will appear here...