how to stop egress either after a transfer is done...
# ask-ai
g
how to stop egress either after a transfer is done or the client leaves the room? We need egress to stop immediately
t
Thanks for reaching out, Aumesh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to stop egress immediately after a SIP transfer completes or when a client leaves the room. You can stop egress right away by calling the StopEgress API — LiveKit does not automatically stop egress on transfer or participant leave. Two simple options: • Stop egress after transfer a. After your
TransferSIPParticipant
call completes, immediately call StopEgress with the egress ID: • JavaScript:
await egressClient.stopEgress(egressId);
• Go:
egressClient.StopEgress(ctx, &livekit.StopEgressRequest{EgressId: egressID})
• CLI:
lk egress stop --id <egress_id>
a. This stops the egress immediately regardless of room/participant state. • Stop egress when a client leaves a. Enable webhooks and listen for
participant_left
(or
room_finished
for room-level egress). b. In your webhook handler, detect if the left participant is tied to the egress and call StopEgress with that egress ID. Recommended best practice: delete the room when the call ends (if possible) — that will cause the egress participant to leave and stop recording. If you can’t delete the room, use the webhook + StopEgress flow to guarantee immediate stop. Would you like the exact webhook payload example or a code sample for your language (JS/Go/Ruby) to plug into your handler? Sources: Call forwarding | LiveKit docs | Egress API | LiveKit docs | Webhooks | LiveKit docs
g
We use python and the livekit agent framework
t
You're using Python with the LiveKit agent framework and want to stop egress immediately after a SIP transfer or when a participant leaves. Below are concise, actionable options and example code you can adapt. Option A — Stop egress right after SIP transfer 1. After your
TransferSIPParticipant
completes, call StopEgress with the egress ID. Example (Python, synchronous style):
Copy code
from livekit import EgressServiceStub, StopEgressRequest
import grpc

def stop_egress(egress_id, channel):
    client = EgressServiceStub(channel)
    req = StopEgressRequest(egress_id=egress_id)
    client.StopEgress(req)
Call
stop_egress(egress_id, channel)
immediately after your transfer call returns. Option B — Stop egress when participant leaves (webhook / agent event) 1. Use the agent framework to handle participant leave events or room finished events. 2. When you detect the participant tied to an egress has left, call StopEgress. Example (agent event handler):
Copy code
from livekit import EgressServiceStub, StopEgressRequest
import grpc

def on_participant_left(event, channel):
    # event should include participant identity and any metadata you set linking to egress_id
    egress_id = event.metadata.get("egress_id")  # adapt to how you track it
    if not egress_id:
        return
    client = EgressServiceStub(channel)
    req = StopEgressRequest(egress_id=egress_id)
    client.StopEgress(req)
Notes and tips • You must track which egress_id corresponds to which participant/session (store in participant metadata, a DB, or in-memory map) so the handler can look it up. • StopEgress stops immediately regardless of room state. • If you create egress from the agent, capture and persist the returned
egress_id
when starting the egress so you can stop it later. If you want, tell me: • how you start egress in your agent (RoomComposite/TrackComposite/custom), and • how you currently track egress IDs —I can provide a tailored code snippet showing where to store the egress_id and how to wire it into your agent event handlers.