steep-balloon-41261
08/10/2025, 3:17 PMtall-airline-16756
08/10/2025, 3:17 PMtall-airline-16756
08/10/2025, 3:18 PMrtc.Room()
method. It’s designed to make real-time communication smooth and comes with handy features like adaptive streaming and automatic subscription. Here’s a quick guide on how to do it:
1. Generate an Access Token: You’ll want to create an access token with the right permissions using the LiveKit API. For example:
token = (
api.AccessToken()
.with_identity("client-user")
.with_name("Client User")
.with_grants(
api.VideoGrants(
room_join=True,
room="your-room-name",
)
)
.to_jwt()
)
1. Connect to the Room: Then, use the rtc.Room()
class to connect with your URL and token like this:
room = rtc.Room()
await room.connect(LIVEKIT_URL, token, options=rtc.RoomOptions(auto_subscribe=True))
This way is pretty straightforward and reliable—it takes care of connection management and event handling for you.
Just a heads-up: the REST API is usually for server-side stuff like creating rooms or managing recordings, not for jumping into rooms in real-time. If you have any questions or want me to walk you through anything else, just give me a shout!tall-airline-16756
08/10/2025, 3:20 PMjob_ctx.connect()
is being called: Double-check that you’re calling job_ctx.connect()
right at the start of your job’s entrypoint to kick off the room connection:
async def job_entry(job_ctx: JobContext) -> None:
await job_ctx.connect() # This sets up the room connection
# Your job logic here
1. Handle shutdown properly: It’s important to call job_ctx.shutdown()
when cleaning up so disconnections happen smoothly:
try:
# Your job logic
finally:
await job_ctx.shutdown("Completed") # Make sure shutdown runs
1. Check your production setup: If you’re running this in production, make sure you have multiple servers per agent to avoid any single points of failure. Following LiveKit’s production deployment tips can really help here.
2. Keep an eye on connection state: You can monitor connection events to handle any disconnects or reconnects gracefully:
room = job_ctx.room
room.on("disconnected", lambda: print("Room connection lost"))
1. Some troubleshooting tips:
◦ Take a look at your logs to see connection attempts and any errors.
◦ Make sure there aren’t any network hiccups or firewall blocks getting in the way.
◦ Check that your server has enough resources to handle the job.
Hopefully, these tips get you back on track with a solid room connection. If you hit any snags or want to chat more about it, just let me know—I’m here to help!
Sources: agents/src/ipc/job_proc_lazy_main.ts | How to Get Help from LiveKit | Web and mobile frontends | LiveKit Docs | Web and mobile frontends | LiveKit Docs | src/room/participant/LocalParticipant.tstall-airline-16756
08/10/2025, 3:24 PMtall-airline-16756
08/10/2025, 3:25 PMtall-airline-16756
08/10/2025, 3:30 PMtoken = (
api.AccessToken()
.with_identity("client-user") # Unique identifier for the client
.with_name("Client User") # Optional display name for the client
.with_grants(
api.VideoGrants(
room_join=True, # Permission to join the room
room="your-room-name", # The specific room the client can join
)
)
.to_jwt() # Converts the token to a JWT string
)
This token is created on the server side to keep things secure, and then you pass it along to the client so they can connect to the room.
1. Connect to the Room: Once you have that token, you’ll use the rtc.Room()
class to actually connect to the LiveKit server. Here’s how you do it:
room = rtc.Room()
await room.connect(LIVEKIT_URL, token, options=rtc.RoomOptions(auto_subscribe=True))
Here’s what’s happening:
• `rtc.Room()`: This creates a new room instance to manage your connection.
• `room.connect()`: This connects you to the LiveKit server using your URL and token.
• `options=rtc.RoomOptions(auto_subscribe=True)`: This makes sure you automatically subscribe to audio and video tracks as soon as you join.
This approach is great because it takes care of managing the connection, retrying if something goes wrong, and handling events like new participants joining or track updates.
If you want me to explain anything more or need some extra examples, just let me know—I’m here to help!tall-airline-16756
08/10/2025, 3:34 PMfrom livekit import api
import os
# Generate random room name and participant identity
room_name = f"my-room-{os.urandom(4).hex()}"
identity = "user1"
# Create an AccessToken with VideoGrants
token = api.AccessToken() \
.with_identity(identity) \
.with_name(identity) \
.with_grants(api.VideoGrants(
room_join=True,
room=room_name,
)).to_jwt()
Node.js Example:
const { AccessToken } = require("livekit-server-sdk");
// Create an access token with VideoGrants
const at = new AccessToken(apiKey, apiSecret, {
identity: username,
});
at.addGrant({
roomJoin: true,
room: roomName,
});
const token = await at.toJwt();
Connecting to a LiveKit Room
Once you have your token, you can use the client SDK to connect. Here’s how:
Python Example:
from livekit import rtc
async def main():
room = rtc.Room()
@room.on("participant_connected")
def on_participant_connected(participant: rtc.RemoteParticipant):
print(f"Participant {participant.identity} connected")
@room.on("track_subscribed")
def on_track_subscribed(track: rtc.Track,
publication: rtc.RemoteTrackPublication,
participant: rtc.RemoteParticipant):
print(f"Subscribed to {track.kind} track from {participant.identity}")
token = "yourGeneratedToken" # From above
ws_url = "<ws://localhost:7880>"
await room.connect(ws_url, token)
print("Connected to room")
asyncio.run(main())
Node.js Example:
const room = new Room();
await room.connect("<ws://localhost:7880>", token);
Community Resources
• Slack Channel: LiveKit Slack — jump in for real-time chats and support.
• GitHub Issues: LiveKit GitHub — great place to ask questions or report issues.
• Documentation: LiveKit Docs — full of guides and examples to explore.
Starter Code
Feel free to check out the LiveKit Examples repo for fully functional apps. If you want a complete token server implementation, the token-server-node example is a perfect place to start.
If you have any questions or want me to walk you through anything else, just let me know — I’m here to help!