have any reference code where there are multiple p...
# ask-ai
m
have any reference code where there are multiple participants and a single agent in the room. Basically confused where Im creating a live authtoken from my python backend, agent connects. I need to manually connect one more remote participant to the same room. Im using meet.livekit to visualise the room stuffs.
t
Thanks for reaching out, AI. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To manage multiple participants and a single agent in a LiveKit room using your Python backend and meet.livekit.io for visualization, you can follow this approach: 1. Backend Token Generation: • Use the LiveKit API to generate tokens for participants and the agent. Each token should include appropriate permissions (e.g., room join, publish, subscribe). • For example, you can create a function to generate participant tokens and another for agent tokens, specifying their unique identities and roles. 2. Room Creation: • Create a room using the LiveKit API before connecting participants and the agent. 3. Join Links: • Generate join links for each participant and the agent using the tokens and your LiveKit URL. These links can be used to connect to the room via meet.livekit.io. 4. Visualization: • Participants and the agent can join the room using the generated links, and meet.livekit.io will handle the visualization. If you need detailed code examples or further assistance, let me know!
m
Now im creating usinfimport sys from pathlib import Path sys.path.append(str(Path(file).parent.parent)) from livekit import api from settings import Settings settings = Settings() # Create the access token first token = api.AccessToken(settings.livekit_api_key, settings.livekit_api_secret) \ .with_identity("identity") \ .with_name("name") \ .with_grants(api.VideoGrants( room_join=True, room="room_name", can_publish=True, can_subscribe=True, can_update_own_metadata=True, )).to_jwt() # Build the URL with the token base_url = f"https://meet.livekit.io/custom?liveKitUrl={settings.livekit_url}&token={token}" print(base_url)
t
Your code for generating a LiveKit access token and building the URL for meet.livekit.io looks spot on! Here’s a quick rundown: 1. Token Generation: • You’re using
api.AccessToken
to create a token with all the right permissions (like
room_join
,
can_publish
,
can_subscribe
, and so on). • The
to_jwt()
method then turns that token into a JWT string. 2. URL Construction: • The URL gets built using your token and LiveKit URL, which is then passed to meet.livekit.io so you can see everything in action. If anything feels off or you want me to walk through any part again, just give me a shout—I’m here to help!
m
Yes...now i need to manually connect another distinct participant to this room
t
If you want to manually connect another participant to the room, here’s a simple way to do it: 1. Generate an Access Token: • Create a new token for the participant using the
AccessToken
class. Make sure to include the necessary permissions like
room_join
,
can_publish
, and
can_subscribe
. 2. Connect the Participant: • Use the token you generated to connect the participant to the room. You can do this through the LiveKit SDK or by sharing the token in a URL for meet.livekit.io. Here’s an example to help you out:
Copy code
from livekit import api

# Generate token for the new participant
token = api.AccessToken(settings.livekit_api_key, settings.livekit_api_secret) \
    .with_identity("new-participant") \
    .with_name("New Participant") \
    .with_grants(api.VideoGrants(
        room_join=True,
        room="room_name",
        can_publish=True,
        can_subscribe=True,
    )).to_jwt()

# Build the URL for <http://meet.livekit.io|meet.livekit.io>
url = f"<https://meet.livekit.io/custom?liveKitUrl={settings.livekit_url}&token={token}>"
print(url)
Just share this URL with your new participant, and they’ll be all set to join the room. If you have any questions or need a hand, feel free to ask!