nice-fish-21757
07/30/2025, 5:46 AMtall-airline-16756
07/30/2025, 5:46 AMtall-airline-16756
07/30/2025, 5:47 AMRuntimeError: room is not connected occurs when operations are attempted on a LiveKit room that is not connected. Here are steps to resolve this issue:
1. Check Connection Status Before Operations
Ensure the room is connected before performing any operations:
if room.isconnected():
# Perform operations
stats = await room.get_rtc_stats()
else:
print("Room is not connected")
2. Ensure Proper Connection
Make sure to connect to the room before using it:
room = Room()
try:
await room.connect("<ws://localhost:7880>", "your_token")
# Now you can safely use room methods
stats = await room.get_rtc_stats()
except ConnectError as e:
print(f"Failed to connect: {e.message}")
3. Handle Connection State Changes
Listen for connection state changes to handle disconnections:
@room.on("disconnected")
def on_disconnected(reason):
print(f"Room disconnected: {reason}")
@room.on("connected")
def on_connected():
print("Room connected successfully")
4. Wait for Connection in Utility Functions
Ensure the room is connected before using utility functions like `wait_for_participant()`:
if not room.isconnected():
await room.connect(url, token)
participant = await wait_for_participant(room, identity="user123")
5. Proper Error Handling
Wrap room operations in try-catch blocks to handle connection errors:
try:
if not room.isconnected():
await room.connect(url, token)
participant = await wait_for_participant(room)
except RuntimeError as e:
if "room is not connected" in str(e):
print("Room connection lost, attempting to reconnect...")
await room.connect(url, token)
else:
raise
By following these steps, you can avoid and handle the "room is not connected" error effectively.
Sources: livekit-agents/livekit/agents/utils/participant.py | livekit-rtc/livekit/rtc/room.py | packages/livekit-rtc/src/room.ts