How do I read participant attributes inside the No...
# ask-ai
q
How do I read participant attributes inside the NodeJS agent / defineAgent() ?
t
Thanks for reaching out, Andrei. 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 read participant attributes inside the NodeJS agent using
defineAgent()
! You can easily access the
attributes
property of participant objects right within your agent's entry function. Here's a helpful example to guide you:
Copy code
export default defineAgent({
  entry: async (ctx: JobContext) => {
    await ctx.connect();

    // Wait for a participant to join
    const participant = await ctx.waitForParticipant();

    // Access participant attributes
    console.log('Participant attributes:', participant.attributes);

    // Access specific attribute values
    const specificAttribute = participant.attributes['your-attribute-key'];
    console.log('Specific attribute:', specificAttribute);
  },
});
Also, if you'd like to access attributes from all participants in the room, here's how you can do that:
Copy code
export default defineAgent({
  entry: async (ctx: JobContext) => {
    await ctx.connect();

    // Access room and its participants
    const room = ctx.room;

    // Iterate through all participants and their attributes
    room.remoteParticipants.forEach((participant) => {
      console.log(`Participant ${participant.identity} attributes:`, participant.attributes);
    });

    // Access local participant attributes
    console.log('Local participant attributes:', room.localParticipant.attributes);
  },
});
Just a little note: participant attributes are a string key-value store that you can set in the participant's access token, and they’ll be synchronized automatically to everyone in the room. If you have any questions or want to dive deeper, please don’t hesitate to ask—I’m here to help! Sources: Rooms, participants, and tracks | LiveKit Docs | src/agent.ts | Job lifecycle | LiveKit Docs | Participant attributes and metadata | LiveKit Docs