Track the Call State in the Browser Client
Monitor and react to real-time conversation state changes during voice calls.
StateChange Event
The StateChange event is emitted whenever the client state changes. It provides both the current state and the previous state for comparison:
Micdrop.on( 'StateChange', (currentState: MicdropState, previousState: MicdropState) => { // Handle state change })See MicdropClient for more details.
State Properties
The MicdropState object contains these properties:
| Property | Type | Description |
|---|---|---|
status | MicdropStatus | Where the call stands, see below |
activity | MicdropActivity | Who has the floor, see below |
isStarting | boolean | Call is connecting and starting |
isStarted | boolean | Call is active and ready |
isReconnecting | boolean | Call is attempting to reconnect |
isMuted | boolean | Call is muted, and still running |
isPaused | boolean | Call is paused (no audio processing) |
isListening | boolean | Ready to detect user speech |
isProcessing | boolean | Processing user input on server |
isUserSpeaking | boolean | User voice detected |
isAssistantSpeaking | boolean | Assistant audio playing |
isMicStarted | boolean | Microphone is active |
isMicMuted | boolean | Microphone is muted |
micDeviceId | string | Active microphone device ID |
speakerDeviceId | string | Active speaker device ID |
micDevices | MicdropDevice[] | Available microphones |
speakerDevices | MicdropDevice[] | Available speakers |
conversation | MicdropConversation | Message history |
partialAssistantMessage | string | Answer being written, if streamed |
error | MicdropClientError | Current error if any |
Status and Activity
Two properties hold the whole state of a call, each with one value at a time. The booleans above spell them out, and stay handy for a quick check.
status follows the call itself:
| Value | Meaning | Booleans |
|---|---|---|
stopped | No call. The microphone may still be on, for a test | |
starting | Opening the microphone and the connection | isStarting |
started | The call is on | isStarted |
reconnecting | The connection dropped, and the call waits for a new one | isStarted, isReconnecting |
activity says who has the floor:
| Value | Meaning | Boolean |
|---|---|---|
idle | Nothing to hear nor to wait for: stopped, paused or muted | |
listening | Waiting for the user to speak | isListening |
userSpeaking | The user speaks | isUserSpeaking |
processing | The server prepares the answer | isProcessing |
assistantSpeaking | The answer is playing | isAssistantSpeaking |
A switch on them covers every case once:
Micdrop.on('StateChange', (state) => { if (state.status === 'reconnecting') return showBanner('Reconnecting…')
switch (state.activity) { case 'listening': return showIndicator('🎤 Listening') case 'userSpeaking': return showIndicator('🗣️ You are speaking') case 'processing': return showIndicator('⚡ Thinking') case 'assistantSpeaking': return showIndicator('🤖 Speaking') case 'idle': return hideIndicator() }})State Diagram
stateDiagram-v2
[*] --> isStarting: Start call
isStarting --> isStarted: Start mic and connection
isStarted --> [*]: Stop call
state isStarted {
[*] --> isProcessing
isListening --> isUserSpeaking : User speaks
isUserSpeaking --> isProcessing : User stops speaking
isProcessing --> isAssistantSpeaking : Generate and play answer
isProcessing --> isUserSpeaking : User interrupts
isProcessing --> isListening : No answer generated
isAssistantSpeaking --> isListening : Assistant finished
isAssistantSpeaking --> isUserSpeaking : User interrupts
}
isStarted --> isPaused : Pause call
isPaused --> isStarted : Resume call
Notes:
- User can interrupt the assistant at any time by speaking during the
isProcessingorisAssistantSpeakingstates. This allows for natural conversation flow. To prevent interruptions during assistant responses, use thedisableInterruptionsetting. Speech heard during the first 2 seconds of the first answer is taken for echo rather than an interruption. - When
isPausedis true,isStartedstays true. - When
isReconnectingis true,isStartedstays true. - On a phone,
speakerDeviceIdis a route rather than a device,'speaker'or'earpiece'. See Audio output and devices. isMicStartedbecomes true when we start the mic (withMicdrop.startMic) or start the call (withMicdrop.start).
Starting a Call
Micdrop.on('StateChange', (state, prevState) => { if (state.status === prevState.status) return
if (state.status === 'starting') { console.log('🔄 Connecting...') } else if (state.status === 'started') { console.log('✅ Connected and ready!') } else if (state.status === 'reconnecting') { console.log('🔄 Connection lost, reconnecting...') }})Conversation Flow
The typical conversation flow follows this pattern:
Micdrop.on('StateChange', (state, prevState) => { // 1. Listening for user input if (state.isListening && !prevState.isListening) { console.log('🎤 Listening...') // Show listening indicator }
// 2. User starts speaking if (state.isUserSpeaking && !prevState.isUserSpeaking) { console.log('🗣️ User speaking...') // Show speaking indicator }
// 3. Processing user input if (state.isProcessing && !prevState.isProcessing) { console.log('⚡ Processing...') // Show processing indicator }
// 4. Assistant responds if (state.isAssistantSpeaking && !prevState.isAssistantSpeaking) { console.log('🤖 Assistant speaking...') // Show assistant speaking indicator }})With React
For React applications, use the useMicdropState hook to automatically subscribe to state changes.
First, install the React package:
npm install @micdrop/reactThen use the hook:
import { useMicdropState } from '@micdrop/react'
function CallInterface() { const { isStarting, isStarted, isReconnecting, isListening, isUserSpeaking, isProcessing, isAssistantSpeaking, isPaused, } = useMicdropState()
return ( <div className="call-interface"> {isStarting && <div className="status starting">Starting...</div>} {isStarted && <div className="status started">Started</div>} {isReconnecting && ( <div className="status reconnecting">🔄 Reconnecting...</div> )} {isPaused && <div className="status paused">⏸️ Call paused</div>} {isMuted && <div className="status muted">⏸️ Call muted</div>} {isListening && <div className="status listening">🎤 Listening</div>} {isUserSpeaking && ( <div className="status user-speaking">🗣️ You're speaking</div> )} {isProcessing && <div className="status processing">⚡ Processing</div>} {isAssistantSpeaking && ( <div className="status assistant-speaking">🤖 Assistant speaking</div> )} </div> )}More information about hooks in the React hooks section.