How can i pass metadata like jwt authentication to...
# ask-ai
a
How can i pass metadata like jwt authentication token from the front end from a user to the agent when connecting to a room
t
Thanks for reaching out, David. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you please share a bit more about how you're currently connecting to the room? For instance, are you using a particular SDK or API? Also, are you hoping to pass the metadata as part of the connection request, or would you prefer to do it another way? Understanding this will really help me guide you better.
a
I’m using react sdk livekitroom component and i am trying to pass the metadata with connection options its a database jwt token if there is a better way to do it let me know
t
To pass metadata like a JWT authentication token using the React SDK's LiveKitRoom component, you’ll want to use the token prop. This token acts as a JWT-based access key that helps authenticate the client to the LiveKit room, carrying important details like participant identity, room name, permissions, and expiration time. It’s important to generate the token securely on your server using your LiveKit API key and secret, then pass it safely to the client. Remember, this token isn’t passed through the connectOptions prop but directly via the token prop. Here’s a simple example to guide you:
Copy code
<LiveKitRoom 
  serverUrl="wss://your-livekit-server" 
  token="your-jwt-access-token" 
  connectOptions={{ autoSubscribe: true }} 
  onConnected={() => { console.log("Connected to room"); }}
>
  {/* Child components */}
</LiveKitRoom>
Make sure your token includes the right grants, like room join, publish, and subscribe, tailored to the participant’s role. You’ll want to get this token from your backend authentication system before rendering the LiveKitRoom component. If you have any questions along the way, feel free to ask—I’m here to help! Sources: Authentication | LiveKit Docs | README.md | packages/react/src/components/LiveKitRoom.tsx
a
The jwt token i am trying to pass is for supabase This is for a tool functionality i want to use not a token to connect to the room
t
To pass custom metadata like a Supabase JWT token for tool functionality, you’ll want to include it in the participant metadata when you generate the LiveKit access token on your server. This metadata gets embedded right into the token and can be accessed easily on the client side. ### Steps: 1. Embed Metadata in the Access Token (Server Side) When you generate the access token using the LiveKit server SDK, just add your Supabase JWT in the
metadata
field. Here’s a simple example in Node.js:
Copy code
import { AccessToken } from 'livekit-server-sdk';

const at = new AccessToken('api-key', 'api-secret', {
  identity: 'user123',
  metadata: JSON.stringify({
    supabaseToken: 'your-supabase-jwt',
    // other custom data
  }),
});

at.addGrant({ room: 'my-room', roomJoin: true, canPublish: true, canSubscribe: true });

const token = await at.toJwt();
You’ll then send this token to the client, where it’s used in the
LiveKitRoom
component. 2. Access Metadata on the Client Once connected, you can grab the metadata from the participant object in your React app like this:
Copy code
import { useParticipantContext } from '@livekit/components-react';

const ParticipantInfo = () => {
  const participant = useParticipantContext();
  const metadata = participant.metadata; // This includes your Supabase JWT
  return <div>Metadata: {metadata}</div>;
};
Important Notes: • Keep in mind metadata is limited to 64 KiB. • Make sure to handle token expiration and revocation properly for security. This method helps ensure your Supabase JWT is passed securely and is ready to be used for your tool’s functionality. If you have any questions or need further help, feel free to ask! Sources: examples/nextjs/pages/processors.tsx | docs/interfaces/LiveKitRoomProps.html | web/src/app/api/token/route.ts | Room metadata | LiveKit Docs | Authentication | LiveKit Docs
a
Show me in python not node
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!