Skip to Content
← Back to all demos

Session Management

Control chat sessions programmatically with startNewSession() and track session lifecycle events

Initialize with sessionIdGenerate UUIDsstartNewSession() APISession eventsEvent monitoringProgrammatic control

Current Session

No active session

Session Controls

Start New Session with UUID

Enter a UUID or generate one, then start a new session with that specific ID:

Other Actions

Tip: clearChat() ends the session but doesn't start a new one until the next message. startNewSession() immediately creates a new session.

clearChat() vs startNewSession()

MethodEnds SessionClears MessagesStarts New SessionShows WelcomeReturns SessionId
clearChat()YesYesNo (on next msg)NoNo
startNewSession()YesYesYes (immediate)YesYes

Integration Code

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

🟢sessionStart- Session created
🔴sessionEnd- Session ended
🟡sessionClear- User cleared

Session events will appear here...

Use Cases

  • Resume sessions after page reload (store sessionId in localStorage)
  • Coordinate multiple widgets sharing the same session
  • Link widget session to your application's user session
  • Initialize widget with session created via API
  • Programmatically reset conversations with specific IDs
  • Track session lifecycle for analytics and monitoring