I am using the Unity engine with a realtime agent....
# ask-ai
b
I am using the Unity engine with a realtime agent. I would like to use a data track to send logging text from the Python layer to my Unity app for debugging. How do I do that, in Python and in C# on the Unity side?
t
Thanks for reaching out, Mat. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’ll show exactly how to send debug/log text from a Python LiveKit agent to a Unity client via a data track, with minimal but complete examples and a few practical notes. Acknowledging: you want Python-side sending and Unity C# receiving for debugging. 1) Python (agent) — send text messages • Use
ctx.room.local_participant.publish_data()
to publish text. Use
topic
(e.g., "debug-log") so Unity can filter.
Copy code
# Python (LiveKit agent)
from livekit.agents import JobContext

async def send_debug_log(ctx: JobContext, message: str):
    await ctx.room.local_participant.publish_data(
        payload=message,      # text payload
        topic="debug-log",    # optional topic name
        reliable=True,        # reliable delivery for logs
    )

# Example use inside an agent session
@server.rtc_session()
async def my_agent(ctx: JobContext):
    await ctx.connect()
    await send_debug_log(ctx, "Agent started and connected.")
    # ...later...
    await send_debug_log(ctx, "Processing audio chunk #5")
2) Unity C# — receive text messages • Register a text stream handler for the same topic or subscribe to raw data events depending on SDK usage. Option A — Text stream handler (topic-based):
Copy code
using UnityEngine;
using LiveKit; // your Unity LiveKit SDK namespace

public class DebugLogReceiver : MonoBehaviour
{
    Room room;

    async void Start()
    {
        room = new Room();
        await room.Connect("<wss://your-livekit-url>", "YOUR_TOKEN");

        // Register handler for text messages on "debug-log"
        room.RegisterTextStreamHandler("debug-log", OnDebugTextReceived, null);
    }

    void OnDebugTextReceived(TextStreamReader reader, string participantIdentity)
    {
        string message = reader.Read();
        Debug.Log($"[DebugLog from {participantIdentity}]: {message}");
        // Update UI or store message if desired
    }

    void OnDestroy()
    {
        if (room != null)
            room.UnregisterTextStreamHandler("debug-log", OnDebugTextReceived, null);
    }
}
Option B — Raw data channel event (if SDK exposes DataReceived):
Copy code
room.DataReceived += (sender, e) =>
{
    string msg = System.Text.Encoding.UTF8.GetString(e.Data);
    Debug.Log($"[RawData from {e.Participant.Identity}]: {msg}");
};
Practical notes and tips • Use the same
topic
string on both sides (e.g., "debug-log"). • Use
reliable=True
in Python to ensure delivery for debugging messages. • If you’ll emit many messages, consider batching or switching to lossy mode for non-critical high-frequency logs to avoid bandwidth/backpressure issues. • Clean up/unregister handlers when the Unity object is destroyed to avoid leaks. • For WebGL builds confirm you are using the appropriate LiveKit Unity SDK build target. If you want, tell me: • which Unity LiveKit SDK/version you’re using (so I can tailor exact APIs), and • whether you prefer topic-based text streams or raw data events — I’ll provide any small adjustments needed. Sources: Job lifecycle | LiveKit docs | Transcriber | LiveKit docs | client-sdk-unity-web | livekit/livekit_composite | DeepWiki | Text and transcriptions | LiveKit docs