I use realtime multimodal agent, node.js, when I s...
# ask-ai
h
I use realtime multimodal agent, node.js, when I start a new session it works, I use this settings: await session.start({ room: ctx.room, agent: new Agent(), inputOptions: { noiseCancellation: BackgroundVoiceCancellation(), closeOnDisconnect: false, }, }); I do not want to close on disconnect, because I want user to be able to reconnect and continue in discussion after for example browser refresh, but how to do it, do I need to always run session.start again? close the previous session? on reconnected event?
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.
To help you smoothly handle reconnection in your LiveKit realtime multimodal agent without closing the session, here’s a friendly guide you can follow: 1. First, make sure the
closeOnDisconnect
option is set to `false`—it sounds like you’ve already got this part covered, which is great! 2. When a reconnection happens, there’s no need to manually close the previous session. Instead, you can listen for reconnection events and pick up right where you left off. Here’s a simple example:
Copy code
session.on('session_reconnected', () => {
  console.log('Session reconnected successfully');
  // Resume any paused operations or regenerate responses if needed
});
If the connection to the LiveKit server drops, don’t worry—the SDK will try to reconnect automatically. You can also handle these events at the room level like this:
Copy code
room.on(RoomEvent.Reconnecting, () => {
  console.log('Reconnecting to the room...');
});

room.on(RoomEvent.Reconnected, () => {
  console.log('Reconnected to the room successfully');
});
One more tip: make sure your backend supports session persistence (for example, using Redis) so that the reconnection keeps everything intact without losing any state. If you have any questions or need a hand implementing this, just let me know—I’m here to help! Sources: plugins/google/src/beta/realtime/realtime_api.ts | CHANGELOG.md
h
how to make session persistent using redis
t
To enable session persistence using Redis in LiveKit, let me guide you through the steps: 1. Configure Redis in LiveKit's config.yaml: You’ll want to add this block to your
config.yaml
file:
Copy code
redis:
  address: redis.host:6379
  # Optional fields:
  # username: myuser
  # password: mypassword
  # db: 0
If you have a more advanced setup, feel free to configure TLS, Sentinel, or Cluster options as needed. 1. Ensure Related Services Use the Same Redis: It’s important that services like Ingress, Egress, and SIP all point to the same Redis instance to keep everything consistent. Here’s an example:
Copy code
redis:
  address: redis.host:6379
1. Verify Redis Connectivity: When you start LiveKit, check your logs to make sure Redis is connecting properly. You should see messages like "connecting to redis." 2. Use Proper Networking: If you’re running this in Docker, make sure Redis is reachable by using
host.docker.internal
for Mac/Windows or
172.17.0.1
for Linux. 3. Test the Setup: Try using tools like
livekit-cli
to create rooms or sessions and confirm they persist in Redis. Once you’ve got this set up, Redis will handle storing session data such as rooms, participants, and ingress/egress states, helping you achieve session persistence and high availability. If you have any questions or need a hand with any step, just let me know—I’m here to help! Sources: redis/redis.go | Self-hosting the Ingress Service | LiveKit Docs | Distributed Setup | LiveKit Docs | pkg/service/redisstore_sip.go | test/config-sample.yaml
h
I always receive /usr/src/app/node_modules/@livekit/agents/src/stream/deferred_stream.ts:52 livekit-1 | throw new Error('Stream source already set'); livekit-1 | ^ livekit-1 | livekit-1 | livekit-1 | Error: Stream source already set after reconnecting
t
Could you please share a bit more about how you’re handling the reconnection process? For example: • Are you manually re-initializing the session or stream after reconnecting? • Do you have any custom logic in place to manage streams during the reconnection? • Are you using any specific event handlers to handle reconnections? Understanding these details will really help us pinpoint what’s causing the 'Stream source already set' error and find the best way to resolve it together.
h
no custom logic, no specific event handlers for reconnections, I do not reinitialize session
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!