In which order in the openai realtime agent, node....
# ask-ai
h
In which order in the openai realtime agent, node.js, should I run session.start, ctx.connect, ctx.waitForParticipant
t
Thanks for reaching out, Pavel. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're reaching out about this! To make sure everything runs smoothly with the OpenAI realtime agent in Node.js, here's the best order to follow: 1.
await ctx.connect()
- This step connects you to the LiveKit room, setting the stage for everything else. 2.
await ctx.waitForParticipant()
- This waits until a participant joins the room, so you're ready to interact. 3.
await agent.start(ctx.room, participant)
- Finally, this starts the session and kicks off the interaction between the agent and the participant. Sticking to this sequence really helps ensure everything initializes properly and your interaction flows just right. If you have any questions or need further help, I'm here for you! Sources: src/agent.ts | src/agent.ts | packages/basics/src/listen-and-respond.ts
h
So I did that, agents is running, participant is there, but when participant refresh the browser, I get throw new Error('Stream source already set');
t
Thanks for reaching out! To better assist you, could you share a bit more about how the agent and participant are managed when the participant refreshes their browser? For instance: • Are you re-initializing the agent or participant after the refresh? • Is the agent session being restarted or reused? • Do you have any specific code in place to handle reconnections? Understanding these details will really help us get to the bottom of the 'Stream source already set' error together. Looking forward to your reply!
h
I ask for a new token on front end (after refresh) with the same agent name and same room name.
t
It looks like the "Stream source already set" error happens because the input/output streams from your previous session aren’t fully cleaned up before reconnecting. Don’t worry, I’m here to help you through this! Here’s what you can do: 1. Ensure Proper Cleanup: Make sure to always call
disconnect()
on the
Room
instance when a participant leaves or refreshes their browser. This step helps release all tracks and subscriptions properly. 2. Reset Streams: Try to avoid reusing input/output streams from your previous session. It’s important to explicitly release them before starting a new session. 3. Wait for Reconnection: After a browser refresh, give it a moment and wait for the
Reconnected
event before setting up streams again. This helps prevent setting things up too early. 4. Use Unique Identifiers: If you can, use unique room or session identifiers. This helps avoid state conflicts when you’re reusing the same room and agent name. Following these steps should help clear up the issue. If you’d like, I’m more than happy to provide specific code examples or assist you further—just let me know! Sources: livekit-agents/livekit/agents/voice/room_io/_input.py | Next.js quickstart | LiveKit Docs | Connecting to LiveKit | LiveKit Docs | Avoiding Room ID Reuse When Re-Creating Rooms With the Same Name
h
what if I reconnect,can I stay in the same session
t
Absolutely! A participant can definitely stay in the same session after reconnecting in LiveKit. LiveKit is designed to support automatic reconnection, so your session stays seamless. Here’s a little overview of how it works: 1. If the connection is interrupted, LiveKit tries to smoothly resume by reconnecting the signaling WebSocket and restarting ICE for the WebRTC connection, minimizing any disruption. 2. If the ICE restart doesn’t succeed, LiveKit moves to a full reconnection. During this time, you’ll see a
Reconnecting
event, and once everything is back on track, a
Reconnected
event is triggered. 3. Importantly, the participant’s identity and session state stay intact, and their tracks are automatically republished after reconnecting. This way, even if there’s a brief network hiccup or a browser refresh, the participant can continue in the same session without missing a beat. Sources: Authentication | LiveKit Docs | src/room/events.ts | Docs::Home
h
Great, but with this setup in play, I reconnect, and I get Stream source already set
t
It looks like you're encountering the "Stream source already set" error because the same stream source is being assigned more than once during reconnection. No worries—here’s how you can smoothly avoid this: 1. Try not to set the stream source again: LiveKit’s built-in reconnection features actually keep your existing stream settings intact. So, there’s no need to reinitialize or reassign the stream source when reconnecting. 2. Consider using quick reconnection mode: This handy mode keeps your current session alive and reuses existing peer connections and published tracks, which helps maintain everything without resetting your streams. 3. If you do need to update stream data, detach sources first: Just call
detachSource()
on your stream before setting a new source. Or, you can create a new
DeferredReadableStream
instance instead of reusing the old one. By following these steps and leaning on LiveKit’s reconnection logic, you’ll be able to prevent this error from popping up. If you’d like, I’m here to help with more details or code examples—just let me know! Sources: agents/src/stream/deferred_stream.test.ts | Sources/LiveKit/Core/Room+Engine.swift | livekit/src/rtc_engine/mod.rs | src/room/RTCEngine.ts
h
how to detachSource in node.js
t
Hey there! If you’re looking to detach a source in Node.js for LiveKit streams, you can use the
detachSource()
method from the
DeferredReadableStream
class. Let me walk you through a simple example: Sources: agents/src/voice/io.ts
h
I tried. I received "Source not set" and immediately after than, error "Source already set"
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!