how to navigate data from python livekit agent to ...
# ask-ai
a
how to navigate data from python livekit agent to react js code i want to navigate webiste name from python to react js to navigate me to website
t
Thanks for reaching out, Yousef. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to send a website URL from a Python LiveKit agent to your React app so the frontend can navigate there. I can help — a few quick questions so I give the exact code you need: 1. Are the agent and the React client connected to the same LiveKit room? 2. Do you want this sent once (single navigation) or repeatedly/streamed? 3. Do you control the React code (can you add listeners or register RPC methods)? 4. Which LiveKit client SDK does your React app use (browser JS, React SDK wrapper, Next.js)? If you confirm those, I’ll provide the minimal Python + React example using the best method (recommended:
sendText()
for a simple URL, or RPC if you want an explicit "navigate" call). Sources: AI Agents Framework | livekit/livekit_composite | DeepWiki | Sending text | LiveKit docs | Data packets | LiveKit docs | Remote method calls | LiveKit docs | Sending files & bytes | LiveKit docs
a
1. Are the agent and the React client connected to the same LiveKit room? Yes 2. Do you want this sent once (single navigation) or repeatedly/streamed?single navigation 3. Which LiveKit client SDK does your React app use (browser JS, React SDK wrapper, Next.js)? Next.js 4. Do you control the React code (can you add listeners or register RPC methods)? listeners
t
Great — since you're in the same room, want a one-time navigation, and can add listeners in your Next.js app, use the text stream (
sendText()
) approach. It's simple and reliable. 1) Python agent — send the URL once:
Copy code
# send a navigation message (topic is optional but useful to filter)
await room.local_participant.send_text("https://example.com", topic="navigation")
2) Next.js (browser JS) — listen for data and navigate:
Copy code
import { useEffect } from "react";
import { RoomEvent } from "livekit-client";

export default function useAgentNavigation(room) {
  useEffect(() => {
    if (!room) return;

    const handler = (data: Uint8Array, participant, kind) => {
      // only handle navigation topic messages
      if (kind !== undefined && kind !== "reliable") return;
      const decoder = new TextDecoder();
      const url = decoder.decode(data);

      // optional: check topic via participant/data metadata if you emit it,
      // or validate URL before navigating
      if (url && url.startsWith("http")) {
        window.location.href = url;
      }
    };

    room.on(RoomEvent.DataReceived, handler);
    return () => {
      room.off(RoomEvent.DataReceived, handler);
    };
  }, [room]);
}
Notes and small checks: • Use the
topic="navigation"
on send to help filter messages. If your SDK exposes topic metadata differently, confirm you can read it — otherwise validate the string contents before navigating. • Validate the URL (startsWith http/https) to avoid unsafe redirects. • This triggers a full-page navigation via
window.location.href
. If you prefer client-side routing in Next.js, replace with your router (e.g.,
router.push(url)
) after ensuring the URL is internal. If you want, I can provide the exact Next.js example using
next/router
instead of
window.location
, or an RPC-based example so the frontend explicitly accepts navigation calls. Which would you prefer?