Hi, can you show how to use metadata?
# ask-ai
f
Hi, can you show how to use metadata?
t
Thanks for reaching out, Baymurat. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit provides several ways to use metadata for synchronizing and storing application-specific data across rooms and participants: • Participant Metadata: Each participant has a
metadata
string field (usually JSON). You can set or update this using SDK methods such as
await localParticipant.setMetadata(metadata)
in JavaScript. This is ideal for passing information like user profiles, roles, or interaction states. You need the
canUpdateOwnMetadata
permission to update this field. • Participant Attributes: Attributes are key-value string pairs, allowing you to update specific information independently, e.g.,
await localParticipant.setAttributes(attributes)
. • Room Metadata: The room also has a metadata field, which is settable only via server APIs during creation or with admin privileges. This can be used for application-scoped settings such as livestream configuration or global features for all participants. Common use cases include user profiles, hand-raising features, SIP metadata, and application-level room configuration. Metadata and attributes are visible to all participants and synchronize automatically, but are not intended for very frequent updates. For more details or examples in a specific programming language or use case, let me know what you’d like to see. References available upon request. Sources: Room metadata | LiveKit Docs | Job lifecycle | LiveKit Docs | src/room/participant/LocalParticipant.ts | SIP dispatch rule | LiveKit Docs | Runtime/Scripts/Participant.cs | livekit-rtc/livekit/rtc/participant.py | lib/src/participant/local.dart | Shared/Types/RoomMetadata.swift | sample-livestream/src/main/java/io/livekit/android/sample/livestream/room/s... | Participant attributes and metadata | LiveKit Docs
f
how to get metadata? is this correct way of getting it? room_metadata = json.loads(ctx.room.metadata)
t
Hey there! You're totally on the right track with handling room metadata. In a LiveKit agent, here's the friendly way to grab that metadata:
Copy code
room_metadata = json.loads(ctx.room.metadata)
Think of this like translating the room's secret notes from a JSON string into a Python dictionary—super handy for getting the info you need! Here are some helpful tips to keep in mind: • Room metadata lives in
ctx.room.metadata
. • Use
json.loads()
to decode that string into something you can work with. • Pro tip: Always double-check that
ctx.room.metadata
isn't empty, and have a backup plan for any JSON curveballs. Bonus: If you want to stay in the loop about metadata changes, you can hook into the
room_metadata_changed
event—perfect for staying up-to-date in real-time. Need a more detailed walkthrough or want to see some error handling magic? Just give me a shout! Sources: Job lifecycle | LiveKit Docs | agent/game_host.py | livekit-protocol/livekit/protocol/room.pyi | Room metadata | LiveKit Docs