@session.on("disconnected")         async def on_r...
# ask-ai
p
@session.on("disconnected")         async def on_room_disconneted():             print(" Room disconnected. Closing logs.")             if not ctx.room.isconnected:                 transcription_logger.close_transcription()                 caller_info.close(current_call_id)
t
Thanks for reaching out, arpan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Handling room disconnection events with the LiveKit Python SDK is pretty straightforward, thanks to the event system in the
Room
class. Let me walk you through how you can set it up: 1. Keep an Ear Out for These Events: ◦ `disconnected`: This one pops up when the room disconnects. Just use
room.on("disconnected")
to catch it. ◦ `reconnecting`: This fires when the SDK is trying to reconnect. Handle it with
room.on("reconnecting")
. ◦ `reconnected`: When the room successfully reconnects, this event is triggered. Use
room.on("reconnected")
for that. 1. *Here’s a Sample to Get You Started:*`import asyncio import logging from livekit import rtc room = rtc.Room() @room.on("disconnected") def on_disconnected(reason): logging.info(f"Disconnected from room: {reason}") @room.on("reconnecting") def on_reconnecting(): logging.info("Attempting to reconnect...") @room.on("reconnected") def on_reconnected(): logging.info("Reconnected to the room") async def main(): token = generate_your_token() await room.connect("ws://your-livekit-server:7880", token) # Your application logic here await asyncio.sleep_forever() if name == "__main__": logging.basicConfig(level=logging.INFO) asyncio.run(main())` 2. A Few Friendly Tips: ◦ The
disconnected
event comes with a
reason
parameter, so you know exactly why the connection dropped—super handy for logging or debugging. ◦ When
reconnecting
fires, it’s a great moment to let your users know you’re working on getting them back online. ◦ And once
reconnected
happens, you can update your UI so everyone knows things are back to normal. By using these events, you’re making sure your app handles disconnections smoothly and keeps your users in the loop, which is always a win! Sources: livekit-rtc/livekit/rtc/_proto/room_pb2.pyi
p
the prolem is disconnection i want to dissconnected room wen no one prasent
t
Hey there! If you want to disconnect a LiveKit room when no one’s in it, here are a couple of easy ways to do that: 1. Server-side Room Deletion: You can use the
DeleteRoom
API to delete the room, which will disconnect everyone inside. Here’s a quick example in Go:
Copy code
_, _ = roomClient.DeleteRoom(context.Background(), &livekit.DeleteRoomRequest{
    Room: "myroom",
})
2. Automatic Disconnection: Set the room’s
EmptyTimeout
so it automatically closes after being empty for a bit. This way, the room shuts down when no participants are around. Both of these options help make sure the room closes properly and frees up resources when it’s empty. If you want a hand setting any of this up, just let me know—I’m happy to help! Sources: pkg/rtc/room.go | Managing rooms | LiveKit Docs | Rust SDKs | livekit/livekit_composite | DeepWiki | src/room/Room.ts | Sources/LiveKit/Core/Room.swift
p
no the thing is want to store transcription in databse with custom function
t
Here’s a friendly guide to help you store LiveKit transcription data in a database using a custom function: 1. Access Transcription Data: Start by listening for the
RoomEvent.TranscriptionReceived
event in your app to grab those transcription segments as they come in. 2. Set Up Database Schema: You’ll want to create a database schema to hold your transcription data. For example, in SQLite:
Copy code
CREATE TABLE participants (
    id TEXT PRIMARY KEY,
    identity TEXT NOT NULL,
    name TEXT
);

CREATE TABLE transcriptions (
    id TEXT PRIMARY KEY,
    participant_id TEXT NOT NULL,
    segment_id TEXT NOT NULL,
    text TEXT NOT NULL,
    language TEXT,
    final BOOLEAN,
    first_received_time INTEGER,
    last_received_time INTEGER,
    FOREIGN KEY(participant_id) REFERENCES participants(id)
);
3. Implement Custom Function: Next, write a function that processes and saves the transcription data to your database. Here’s an example in JavaScript: `async function storeTranscription(transcription) { const db = new sqlite3.Database('transcriptions.db'); await db.run(
CREATE TABLE IF NOT EXISTS participants (id TEXT PRIMARY KEY, identity TEXT NOT NULL, name TEXT)
); await db.run(
CREATE TABLE IF NOT EXISTS transcriptions (id TEXT PRIMARY KEY, participant_id TEXT NOT NULL, segment_id TEXT NOT NULL, text TEXT NOT NULL, language TEXT, final BOOLEAN, first_received_time INTEGER, last_received_time INTEGER, FOREIGN KEY(participant_id) REFERENCES participants(id))
); const participantStmt = db.prepare('INSERT OR IGNORE INTO participants VALUES (?, ?, ?)'); participantStmt.run(transcription.participant_identity, transcription.participant_identity, transcription.participant?.name || ''); participantStmt.finalize(); const transcribeStmt = db.prepare(
INSERT OR REPLACE INTO transcriptions VALUES (?, ?, ?, ?, ?, ?, ?, ?)
); for (const segment of transcription.segments) { transcribeStmt.run(transcription.track_sid, transcription.participant_identity, segment.id, segment.text, segment.language, segment.final, segment.start_time, segment.end_time); } transcribeStmt.finalize(); db.close(); }` 4. Handle Real-Time Data: Set up event listeners to catch and process transcription data as it arrives:
Copy code
room.on(RoomEvent.TranscriptionReceived, (event) => {
    const transcription = new Transcription(event.participant.identity, event.track_sid, event.segments);
    storeTranscription(transcription);
});
5. Best Practices: ◦ Wrap your database actions in transactions to keep things consistent. ◦ Add retry logic to handle any hiccups during database operations. ◦ Use
INSERT OR IGNORE
or
INSERT OR REPLACE
to gracefully manage duplicates. If you’d like, I’m here to help you tweak this for your specific database setup or answer any questions you have along the way. Just let me know! Sources: lib/src/context/transcription_context.dart | Sending text | LiveKit Docs
p
how to check if any participent present or not
t
Hey there! If you want to check whether there are any participants in a LiveKit room, here are a few easy ways to do it: 1. Server-Side: You can use the
RoomServiceClient
to get a list of participants in a room. Here's a quick example:
Copy code
const roomService = new RoomServiceClient(livekitHost, apiKey, apiSecret);
const participants = await roomService.listParticipants(room);
const participantCount = participants.length;
2. Client-Side: On the client side, you can check the participants through the SDK’s room state. For instance, in Python:
Copy code
if room.num_participants > 0:
    print("There are participants in the room")
3. Real-Time Updates: Want to stay updated as participants join or leave? You can subscribe to participant events. Here’s how you might do it in React Native:
Copy code
const participants = useParticipants();
useEffect(() => {
  setParticipantCount(participants.length);
}, [participants]);
4. Command-Line Interface (CLI): If you prefer the command line, the LiveKit CLI lets you list participants in a room like this:
lk room participants list --room <room-name>
If you want a hand setting any of this up, just give me a shout—I’m here to help! Sources: src/components/RoomInfo.tsx | docs/interfaces/RoomState.html | livekit-rtc/livekit/rtc/room.py | src/components/stream-player.tsx