How do I get data from JobContext at runtime, afte...
# ask-ai
c
How do I get data from JobContext at runtime, after await ctx.connect() was called?
t
Thanks for reaching out, Anton. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring how to access data from
JobContext
at runtime after calling
await ctx.connect()
. You can interact with several helpful properties and methods exposed by the
JobContext
object. Let me walk you through some of the key elements: •
room
: This is an instance of
rtc.Room
, allowing you to interact with the LiveKit room, including participants and tracks. •
agent
: Represents the local participant (that’s you, the agent) once connected to the room. •
job
: Contains valuable information about the current job, like metadata for configuration or user-specific data. •
worker_id
: This is the ID of the worker running the job, handy for tracking purposes. •
proc
: Gives you access to process-specific state through
JobProcess.userData
, which is great for storing persistent data during the job lifecycle. Here’s a simple example in Python to illustrate these points:
Copy code
async def entrypoint(ctx: JobContext):
    await ctx.connect()  # Establish connection to the room

    # Access the room and agent (local participant)
    room = ctx.room
    agent = ctx.agent

    # Access job metadata (e.g., passed during dispatch)
    job_metadata = ctx.job.metadata
    print(f"Job metadata: {job_metadata}")

    # Access worker ID
    worker_id = ctx.worker_id
    print(f"Worker ID: {worker_id}")

    # Store and retrieve custom data in process
    ctx.proc.userData["custom_state"] = "example_value"
    print(f"Custom state: {ctx.proc.userData.get('custom_state')}")
If you’d like to dive deeper, I highly recommend checking out the Job Lifecycle documentation. It’s a great resource, and I’m here if you have any questions or need further guidance! Sources: livekit-agents/livekit/agents/ipc/job_proc_lazy_main.py | Job lifecycle | LiveKit Docs