Skip to content

Commit 1b83ae3

Browse files
cursoragentlouis
andcommitted
Adjust room connection timing for better device performance and reliability
Co-authored-by: louis <louis@elevenlabs.io>
1 parent d332d6f commit 1b83ae3

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

Sources/ElevenLabs/Conversation.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ public final class Conversation: ObservableObject, RoomDelegate {
147147
isDevice = true
148148
#endif
149149

150-
// On device, use shorter wait or skip entirely
151-
let waitTime: UInt64 = isDevice ? 20_000_000 : 100_000_000 // 20ms on device, 100ms on simulator
150+
// On device, use LONGER wait for room connection
151+
let waitTime: UInt64 = isDevice ? 200_000_000 : 100_000_000 // 200ms on device, 100ms on simulator
152152

153153
let waitStart = Date()
154154
try? await Task.sleep(nanoseconds: waitTime)
@@ -552,12 +552,12 @@ public final class Conversation: ObservableObject, RoomDelegate {
552552
/// Determine optimal buffer time based on agent readiness pattern
553553
/// Different agents need different buffer times for conversation processing readiness
554554
private func determineOptimalBuffer() async -> TimeInterval {
555-
guard let room = deps?.connectionManager.room else { return 50.0 } // Reduced default buffer
555+
guard let room = deps?.connectionManager.room else { return 200.0 } // Default buffer
556556

557557
// Check if we have any remote participants
558558
guard !room.remoteParticipants.isEmpty else {
559559
print("[ElevenLabs-Timing] No remote participants found, using longer buffer")
560-
return 100.0 // Reduced from 200ms to 100ms
560+
return 300.0 // Longer wait if no agent detected
561561
}
562562

563563
// Check if running on simulator vs device
@@ -568,9 +568,12 @@ public final class Conversation: ObservableObject, RoomDelegate {
568568
isSimulator = false
569569
#endif
570570

571-
// Use shorter buffer on device since data channel setup can be slower
572-
// but we don't want to wait too long
573-
let buffer: TimeInterval = isSimulator ? 150.0 : 50.0
571+
// IMPORTANT: Devices need MORE time, not less!
572+
// On device, data channel and agent initialization is slower due to:
573+
// - Network latency (cellular/wifi vs simulator's ethernet)
574+
// - CPU performance differences
575+
// - Background processes competing for resources
576+
let buffer: TimeInterval = isSimulator ? 100.0 : 250.0 // More time on device!
574577

575578
print("[ElevenLabs-Timing] Determined optimal buffer: \(Int(buffer))ms (simulator: \(isSimulator))")
576579
return buffer

Sources/ElevenLabs/Networking/ConnectionManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class ConnectionManager {
4444
func connect(
4545
details: TokenService.ConnectionDetails,
4646
enableMic: Bool,
47-
graceTimeout: TimeInterval = 0.2 // Reduced from 0.5 to 200ms for faster device startup
47+
graceTimeout: TimeInterval = 0.5 // 500ms to ensure agent is fully ready on slower devices
4848
) async throws {
4949
let room = Room()
5050
self.room = room

0 commit comments

Comments
 (0)