in my react native frontend when to know whether a...
# ask-ai
f
in my react native frontend when to know whether agent is speaking or listening
t
Thanks for reaching out, venu. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to check whether an agent is speaking or listening in your React Native frontend using LiveKit, the
useVoiceAssistant
hook from
@livekit/components-react
is your friend. This handy hook gives you the agent's current state through the
state
property, which could be one of these:
disconnected
,
connecting
,
initializing
,
listening
,
thinking
, or
speaking
. Here’s a quick example to help you get started:
Copy code
import { useVoiceAssistant } from '@livekit/components-react';

function MyComponent() {
  const { state: agentState } = useVoiceAssistant();

  if (agentState === 'speaking') {
    console.log('Agent is currently speaking');
    // You can update your UI here to show the speaking state
  } else if (agentState === 'listening') {
    console.log('Agent is currently listening');
    // And here, update the UI to show the listening state
  }

  return (
    <div>
      <p>Agent State: {agentState}</p>
    </div>
  );
}
Also, just so you know, the agent's state is published as a participant attribute (
lk.agent.state
) on the agent participant. The
useVoiceAssistant
hook keeps this updated in real time, which means you can easily adjust your UI or trigger actions based on what the agent is doing. Hope this helps you out! Sources: AI voice agents | LiveKit Docs | Web and mobile frontends | LiveKit Docs | components/session-view.tsx | complex-agents/drive-thru/frontend/components/session-view.tsx