How to correctly end work after call session in no...
# ask-ai
q
How to correctly end work after call session in nodejs ?
t
Thanks for reaching out, Heorhi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — sounds like you want a concise, correct way to end work after a LiveKit call session in Node.js. Here’s what to do and why. 1) Gracefully disconnect the agent/participant • Call
ctx.shutdown('reason')
to disconnect and allow pending work to finish. This cleanly ends the session from the agent side. 2) Run cleanup logic • Register shutdown callbacks with
ctx.addShutdownCallback(() => { ... })
to persist state, stop timers, close DB connections, free file handles, or remove temp files. 3) (Optional) End the room for everyone • If you want to end the meeting for all participants, call the Room service API to delete the room:
Copy code
const roomServiceClient = new RoomServiceClient(
  process.env.LIVEKIT_URL,
  process.env.LIVEKIT_API_KEY,
  process.env.LIVEKIT_API_SECRET,
);
await roomServiceClient.deleteRoom(roomName);
Quick checklist before shutdown: • Stop or release media tracks and streams if you created them. • Flush or save any queued data or logs. • Close network/db connections and cancel timers. If you want, tell me which parts you’re using (agent SDK, participant client, or room service) and I’ll provide a short code example tailored to your setup. Sources: Job lifecycle | LiveKit docs
q
Is it possible to run code right after await of roomServiceClient.deleteRoom(roomName); ?
t
Short answer: yes — any code placed after
await roomServiceClient.deleteRoom(roomName);
will run once the delete completes. Example:
Copy code
await roomServiceClient.deleteRoom(roomName);
console.log('Room deleted — now running post-delete tasks');
// run cleanup, notify users, close connections, etc.
Notes: • If
deleteRoom
throws, wrap in try/catch to handle errors before running post-delete logic. • If you need to run something regardless of success/failure, use
finally
. • For ordered async work,
await
each step so each completes before the next runs. Want a small example showing try/catch/finally for your exact use case? Sources: Managing rooms | LiveKit docs | Job lifecycle | LiveKit docs