จัดการเซสชันสำหรับ Live API

Gemini Live API จะประมวลผลสตรีมเสียงหรือข้อความต่อเนื่องที่เรียกว่า เซสชัน คุณสามารถจัดการวงจรชีวิตของเซสชันได้ตั้งแต่การเริ่มต้นการเชื่อมต่อครั้งแรกไปจนถึงการสิ้นสุดอย่างราบรื่น

ขีดจำกัดของเซสชัน

สำหรับ Live API เซสชัน หมายถึงการเชื่อมต่อแบบถาวรที่ระบบจะสตรีมอินพุต และเอาต์พุตอย่างต่อเนื่องผ่านการเชื่อมต่อ

หากเซสชันเกินขีดจำกัด ใดก็ตาม ต่อไปนี้ ระบบจะยกเลิกการเชื่อมต่อ อย่างไรก็ตาม Live API มีตัวเลือกบางอย่าง (ดูด้านล่าง) เพื่อ จัดการขีดจำกัดที่เกี่ยวข้องกับเซสชันเหล่านี้

  • หน้าต่างบริบทของเซสชัน จำกัดไว้ที่ 128,000 โทเค็น

    เนื่องจากขีดจำกัดหน้าต่างบริบทนี้ ความยาวเซสชันสูงสุดโดยประมาณตามรูปแบบอินพุตมีดังนี้

    • เซสชันอินพุตเสียงเท่านั้นจำกัดไว้ที่ 15 นาที
    • อินพุตวิดีโอ + เสียงจำกัดไว้ที่ 2 นาที
  • ความยาวการเชื่อมต่อ จำกัดไว้ที่ประมาณ 10 นาที

    คุณจะได้รับการแจ้งเตือน การสิ้นสุดการเชื่อมต่อ ประมาณ 60 วินาที ก่อนที่การเชื่อมต่อจะสิ้นสุด

ตัวเลือกบางอย่างสำหรับการจัดการขีดจำกัดที่เกี่ยวข้องกับเซสชันมีดังนี้

  • บีบอัดหน้าต่างบริบทของเซสชัน เพื่อให้เซิร์ฟเวอร์รักษาขนาดบริบทให้อยู่ภายในขีดจำกัดโดยอัตโนมัติ

  • กลับมาใช้เซสชันต่อ เพื่อป้องกันไม่ให้บริบทการสนทนาหายไประหว่างการตัดการเชื่อมต่อเครือข่ายชั่วคราวหรือ หลังจากได้รับการแจ้งเตือน การสิ้นสุดการเชื่อมต่อ

เริ่มเซสชัน

ดูคู่มือเริ่มต้นใช้งานสำหรับ Live API เพื่อดูข้อมูลโค้ดแบบเต็มที่แสดงวิธีเริ่มเซสชัน

อัปเดตระหว่างเซสชัน

โมเดล Live API รองรับความสามารถขั้นสูงต่อไปนี้สำหรับ การอัปเดตระหว่างเซสชัน:

เพิ่มการอัปเดตเนื้อหาแบบเพิ่มทีละส่วน

คุณสามารถเพิ่มการอัปเดตแบบเพิ่มทีละส่วนระหว่างเซสชันที่ใช้งานอยู่ ใช้ตัวเลือกนี้เพื่อส่งอินพุตข้อความ สร้างบริบทของเซสชัน หรือกู้คืนบริบทของเซสชัน

  • สำหรับบริบทที่ยาวขึ้น เราขอแนะนำให้ระบุข้อมูลสรุปข้อความเดียวเพื่อเพิ่มพื้นที่หน้าต่างบริบทสำหรับการโต้ตอบในภายหลัง

  • สำหรับบริบทสั้นๆ คุณสามารถส่งการโต้ตอบแบบทีละส่วนเพื่อแสดงลำดับเหตุการณ์ที่แน่นอน เช่น ข้อมูลโค้ดด้านล่าง

Swift

// Define initial turns (history/context).
let turns: [ModelContent] = [
  ModelContent(role: "user", parts: [TextPart("What is the capital of France?")]),
  ModelContent(role: "model", parts: [TextPart("Paris")]),
]

// Send history, keeping the conversational turn OPEN (false).
await session.sendContent(turns, turnComplete: false)

// Define the new user query.
let newTurn: [ModelContent] = [
  ModelContent(role: "user", parts: [TextPart("What is the capital of Germany?")]),
]

// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.sendContent(newTurn, turnComplete: true)

Kotlin

Not yet supported for Android apps - check back soon!

Java

Not yet supported for Android apps - check back soon!

Web

const turns = [{ text: "Hello from the user!" }];

await session.send(
  turns,
  false // turnComplete: false
);

console.log("Sent history. Waiting for next input...");

// Define the new user query.
const newTurn [{ text: "And what is the capital of Germany?" }];

// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.send(
    newTurn,
    true // turnComplete: true
);
console.log("Sent final query. Model response expected now.");

Dart

// Define initial turns (history/context).
final List turns = [
  Content(
    "user",
    [Part.text("What is the capital of France?")],
  ),
  Content(
    "model",
    [Part.text("Paris")],
  ),
];

// Send history, keeping the conversational turn OPEN (false).
await session.send(
  input: turns,
  turnComplete: false,
);

// Define the new user query.
final List newTurn = [
  Content(
    "user",
    [Part.text("What is the capital of Germany?")],
  ),
];

// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.send(
  input: newTurn,
  turnComplete: true,
);

Unity

// Define initial turns (history/context).
List turns = new List {
    new ModelContent("user", new ModelContent.TextPart("What is the capital of France?") ),
    new ModelContent("model", new ModelContent.TextPart("Paris") ),
};

// Send history, keeping the conversational turn OPEN (false).
foreach (ModelContent turn in turns)
{
    await session.SendAsync(
        content: turn,
        turnComplete: false
    );
}

// Define the new user query.
ModelContent newTurn = ModelContent.Text("What is the capital of Germany?");

// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.SendAsync(
    content: newTurn,
    turnComplete: true
);

อัปเดตคำแนะนำของระบบระหว่างเซสชัน

ใช้ได้เฉพาะเมื่อใช้ Vertex AI Gemini API เป็น ผู้ให้บริการ API ของคุณ

คุณสามารถอัปเดตคำแนะนำของระบบระหว่างเซสชันที่ใช้งานอยู่ ใช้ตัวเลือกนี้เพื่อปรับการตอบสนองของโมเดล เช่น เปลี่ยนภาษาที่ใช้ตอบสนองหรือปรับโทน

หากต้องการอัปเดตคำแนะนำของระบบระหว่างเซสชัน คุณสามารถส่งเนื้อหาข้อความที่มีบทบาท system คำแนะนำของระบบที่อัปเดตแล้วจะมีผลตลอดระยะเวลาที่เหลือของเซสชัน

Swift

await session.sendContent(
  [ModelContent(
    role: "system",
    parts: [TextPart("new system instruction")]
  )],
  turnComplete: false
)

Kotlin

Not yet supported for Android apps - check back soon!

Java

Not yet supported for Android apps - check back soon!

Web

Not yet supported for Web apps - check back soon!

Dart

try {
  await _session.send(
    input: Content(
      'system',
      [Part.text('new system instruction')],
    ),
    turnComplete: false,
  );
} catch (e) {
  print('Failed to update system instructions: $e');
}

Unity

try
{
    await session.SendAsync(
        content: new ModelContent(
            "system",
            new ModelContent.TextPart("new system instruction")
        ),
        turnComplete: false
    );
}
catch (Exception e)
{
    Debug.LogError($"Failed to update system instructions: {e.Message}");
}

บีบอัดหน้าต่างบริบท

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาเฉพาะของผู้ให้บริการ และโค้ดในหน้านี้

Live API หน้าต่างบริบทของเซสชัน Live API จะจัดเก็บข้อมูลที่สตรีมแบบเรียลไทม์ (25 โทเค็นต่อวินาที (TPS) สำหรับเสียงและ 258 TPS สำหรับวิดีโอ) รวมถึงเนื้อหาอื่นๆ เช่น อินพุตข้อความและเอาต์พุตของโมเดล โมเดล Live API ทั้งหมดมี ขีดจำกัดหน้าต่างบริบทของเซสชันอยู่ที่ 128,000 โทเค็น

โดยค่าเริ่มต้น ความยาวเซสชันสูงสุดโดยประมาณตามรูปแบบอินพุตมีดังนี้เนื่องจากขีดจำกัดหน้าต่างบริบทนี้

  • เซสชันอินพุตเสียงเท่านั้นจำกัดไว้ที่ 15 นาที
  • อินพุตวิดีโอ + เสียงจำกัดไว้ที่ 2 นาที

ในเซสชันที่ทำงานเป็นเวลานาน ประวัติโทเค็นเสียงและ/หรือวิดีโอจะสะสมขึ้นเมื่อการสนทนาดำเนินไป หากประวัติเกินขีดจำกัดของโมเดล โมเดลอาจแสดงข้อมูลที่ไม่ถูกต้อง ทำงานช้าลง หรือระบบอาจบังคับให้เซสชันสิ้นสุดลง

หากต้องการเปิดใช้เซสชันที่ยาวขึ้น คุณสามารถเปิดใช้ การบีบอัดหน้าต่างบริบท ได้โดยตั้งค่าฟิลด์ contextWindowCompression เป็นส่วนหนึ่งของ LiveGenerationConfig เมื่อเปิดใช้แล้ว เซิร์ฟเวอร์จะใช้กลไก หน้าต่างเลื่อน เพื่อทิ้งการโต้ตอบที่เก่าที่สุดหรือสรุปการโต้ตอบเหล่านั้นโดยอัตโนมัติเพื่อรักษาขนาดบริบทให้อยู่ภายในขีดจำกัดเริ่มต้นหรือขีดจำกัดที่ระบุ ระบบจะไม่ทิ้งคำแนะนำของระบบและจะคงคำแนะนำไว้ที่จุดเริ่มต้นของหน้าต่างบริบทเสมอ

จากมุมมองของผู้ใช้ การดำเนินการนี้จะช่วยให้ระยะเวลาของเซสชันเป็นไปได้ไม่จำกัดในทางทฤษฎี เนื่องจากระบบจะจัดการ "หน่วยความจำ" อย่างต่อเนื่อง

คุณสามารถกำหนดค่ากลไกหน้าต่างเลื่อน รวมถึงจำนวนโทเค็นที่ทริกเกอร์การบีบอัด (ไม่บังคับ) (ดูการตั้งค่าและค่าที่ใช้ได้ด้านล่าง) ข้อควรพิจารณาระดับสูงบางประการเกี่ยวกับการใช้การตั้งค่าเหล่านี้มีดังนี้

  • การตั้งค่า targetTokens ให้ต่ำมากจะเพิ่มพื้นที่บริบทสำหรับสตรีมต่อเนื่อง แต่โมเดลจะ "ลืม" การโต้ตอบที่เก่ากว่าอย่างรวดเร็ว

  • การตั้งค่า targetTokens ให้ใกล้เคียงกับ triggerTokens จะช่วยรักษาหน่วยความจำได้มากขึ้น แต่จะทริกเกอร์การทำงานของการบีบอัดบ่อยขึ้นมาก

การตั้งค่า ค่าเริ่มต้นสำหรับหน้าต่างเลื่อนหากไม่ได้ตั้งค่าในการกำหนดค่า ค่าต่ำสุด ค่าสูงสุด
triggerTokens
ความยาวบริบทก่อนที่จะทริกเกอร์การบีบอัด
80% ของขีดจำกัดหน้าต่างบริบทของโมเดล 5,000 128,000
targetTokens
จำนวนโทเค็นเป้าหมายที่จะเก็บไว้
50% ของค่า triggerTokens
  • หากไม่ได้ตั้งค่า triggerTokens อย่างชัดเจน targetTokens จะมีค่าเริ่มต้นเป็น 50% ของค่า triggerTokens เริ่มต้น
  • ค่า targetTokens ต้องน้อยกว่าค่า triggerTokens
0 128,000

Swift


// Initialize the Gemini Developer API backend service
let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "gemini-2.5-flash-native-audio-preview-12-2025",
  // Enable context window compression.
  // (Optional) Configure the number of tokens in the context window that triggers the compression.
  generationConfig: LiveGenerationConfig(
    responseModalities: [.audio],
    contextWindowCompression: ContextWindowCompressionConfig(
      triggerTokens: 10000,
      slidingWindow: SlidingWindow(
        targetTokens: 2000,
      )
    )
  )
)

Kotlin


// Initialize the Gemini Developer API backend service
val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "gemini-2.5-flash-native-audio-preview-12-2025",
    // Enable context window compression.
    // (Optional) Configure the number of tokens in the context window that triggers the compression.
    generationConfig = liveGenerationConfig {
        responseModality = ResponseModality.AUDIO,
        contextWindowCompression = ContextWindowCompressionConfig(
            triggerTokens = 10000,
            slidingWindow = SlidingWindow(targetTokens = 2000)
        )
    }
)

Java


// Initialize the Gemini Developer API backend service
LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
        "gemini-2.5-flash-native-audio-preview-12-2025",
        // Enable context window compression.
        // (Optional) Configure the number of tokens in the context window that triggers the compression.
        new LiveGenerationConfig.Builder()
                .setResponseModality(ResponseModality.AUDIO)
                .setContextWindowCompression(
                        new ContextWindowCompressionConfig(10000, new SlidingWindow(2000))
                )
                .build()
);

Web


const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

const liveModel = getLiveGenerativeModel(ai, {
  model: "gemini-2.5-flash-native-audio-preview-12-2025",
  // Enable context window compression.
  // (Optional) Configure the number of tokens in the context window that triggers the compression.
  generationConfig: {
    responseModalities: [ResponseModality.AUDIO],
    contextWindowCompression: {
      triggerTokens: 10000,
      slidingWindow: {
        targetTokens: 2000,
      },
    },
  },
});

Dart


final _liveModel = FirebaseAI.googleAI().liveGenerativeModel(
  model: 'gemini-2.5-flash-native-audio-preview-12-2025',
  // Enable context window compression.
  // (Optional) Configure the number of tokens in the context window that triggers the compression.
  liveGenerationConfig: LiveGenerationConfig(
    responseModalities: [ResponseModalities.audio],
    contextWindowCompression: ContextWindowCompressionConfig(
      triggerTokens: 10000,
      slidingWindow: SlidingWindow(targetTokens: 2000),
    ),
  ),
);

Unity


var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel(
    modelName: "gemini-2.5-flash-native-audio-preview-12-2025",
    // Enable context window compression.
    // (Optional) Configure the number of tokens in the context window that triggers the compression.
    liveGenerationConfig: new LiveGenerationConfig(
        responseModalities: new[] { ResponseModality.Audio },
        contextWindowCompression: new ContextWindowCompressionConfig(
            triggerTokens: 10000,
            slidingWindow: new SlidingWindow(targetTokens: 2000)
        )
    )
);

ตรวจหาเมื่อเซสชันกำลังจะสิ้นสุด

ระยะเวลาสูงสุดของการเชื่อมต่อ WebSocket แบบต่อเนื่องครั้งเดียวคือประมาณ 10 นาที ระบบจะส่งการแจ้งเตือน การสิ้นสุดการเชื่อมต่อ ไปยังไคลเอ็นต์ 60 วินาทีก่อนที่การเชื่อมต่อจะสิ้นสุด ซึ่งจะช่วยให้คุณดำเนินการ เพิ่มเติมได้ (เช่น โดยการกลับมาใช้เซสชันต่อ)

ตัวอย่างต่อไปนี้แสดงวิธีตรวจหาการยกเลิกการเชื่อมต่อที่กำลังจะเกิดขึ้นโดยการฟังการแจ้งเตือน การสิ้นสุดการเชื่อมต่อ

Swift

for try await response in session.responses {
  switch response.payload {

  case .goingAwayNotice(let goingAwayNotice):
    // Prepare for the session to close soon
    if let timeLeft = goingAwayNotice.timeLeft {
        print("Server going away in \(timeLeft) seconds")
    }
  }
}

Kotlin

for (response in session.responses) {
    when (val message = response.payload) {
        is LiveServerGoAway -> {
            // Prepare for the session to close soon
            val remaining = message.timeLeft
            logger.info("Server going away in $remaining")
        }
    }
}

Java

session.getResponses().forEach(response -> {
    if (response.getPayload() instanceof LiveServerResponse.GoingAwayNotice) {
        LiveServerResponse.GoingAwayNotice notice = (LiveServerResponse.GoingAwayNotice) response.getPayload();
        // Prepare for the session to close soon
        Duration timeLeft = notice.getTimeLeft();
    }
});

Web

for await (const message of session.receive()) {
  switch (message.type) {

  ...
  case "goingAwayNotice":
    console.log("Server going away. Time left:", message.timeLeft);
    break;
  }
}

Dart

Future _handleLiveServerMessage(LiveServerResponse response) async {
  final message = response.message;
  if (message is GoingAwayNotice) {
     // Prepare for the session to close soon
     developer.log('Server going away. Time left: ${message.timeLeft}');
  }
}

Unity

foreach (var response in session.Responses) {
    if (response.Payload is LiveSessionGoingAway notice) {
        // Prepare for the session to close soon
        TimeSpan timeLeft = notice.TimeLeft;
        Debug.Log($"Server going away notice received. Remaining: {timeLeft}");
    }
}

กลับมาใช้เซสชันต่อ

Live API รองรับการกลับมาใช้เซสชันต่อเพื่อป้องกันไม่ให้บริบทการสนทนา หายไป ทุกเซสชันจะมีแฮนเดิล และคุณสามารถใช้แฮนเดิลได้ดังต่อไปนี้

  • รักษาเซสชันก่อนที่จะถึงขีดจำกัดเวลาการเชื่อมต่อ

    ระยะเวลาสูงสุดของการเชื่อมต่อ WebSocket แบบต่อเนื่องครั้งเดียวคือประมาณ 10 นาที คุณสามารถตรวจหาเมื่อการเชื่อมต่อกำลังจะสิ้นสุดได้โดย การฟังการแจ้งเตือน การสิ้นสุดการเชื่อมต่อ จากนั้นขยายเซสชันโดยสร้างการเชื่อมต่อใหม่โดยใช้แฮนเดิลของเซสชัน

  • กลับมาใช้เซสชันต่อทันทีหลังจากที่การเชื่อมต่อขาดหายไป

    หากการเชื่อมต่อสิ้นสุดลงหรือขาดหายไปก่อนที่จะถึงขีดจำกัดเวลาการเชื่อมต่อสูงสุด (เช่น เปลี่ยนจาก Wi-Fi เป็น 5G) เซิร์ฟเวอร์จะเก็บสถานะเซสชันไว้ ประมาณ 10 นาที ในช่วงเวลานี้ คุณสามารถกลับมาใช้เซสชันต่อได้โดยสร้างการเชื่อมต่อใหม่โดยใช้แฮนเดิลของเซสชัน

  • กลับมาใช้เซสชันต่อหลังจากผ่านไประยะเวลานาน

    หลังจากที่การเชื่อมต่อสิ้นสุดลง เซิร์ฟเวอร์จะเก็บสถานะเซสชันไว้ 2-3 ชั่วโมง ในช่วงเวลานี้ คุณสามารถกลับมาใช้เซสชันต่อได้โดยสร้างการเชื่อมต่อใหม่โดยใช้แฮนเดิลของเซสชัน โปรดทราบว่าช่วงเวลานี้จะแตกต่างกันสำหรับ ผู้ให้บริการ Gemini API 2 ราย: Gemini Developer API คือ 2 ชั่วโมง | Vertex AI Gemini API คือ 24 ชั่วโมง

โดยค่าเริ่มต้น ระบบจะปิดใช้การกลับมาใช้เซสชันต่อ หากต้องการเปิดใช้การกลับมาใช้เซสชันต่อ ให้ส่งการกำหนดค่าการกลับมาใช้ต่อที่ว่างเปล่าเมื่อสร้างการเชื่อมต่อใหม่ เมื่อเปิดใช้แล้ว เซิร์ฟเวอร์จะส่งการอัปเดตที่มีแฮนเดิลการกลับมาใช้เซสชันต่อเป็นระยะๆ หากเซสชันถูกตัดการเชื่อมต่อ คุณสามารถเชื่อมต่ออีกครั้งและส่งแฮนเดิลนี้เพื่อกลับมาใช้เซสชันต่อโดยที่บริบทของเซสชันยังคงอยู่

ตัวอย่างต่อไปนี้แสดงตัวเลือก 2 รายการสำหรับการกลับมาใช้เซสชันต่อ

Swift

// Local variable to save the active session handle
var activeSessionHandle: String?

// Initialize the session. Passing an empty config requests the server to send SessionResumptionUpdate
var session = try await liveModel.connect(
  sessionResumption: SessionResumptionConfig()
)

// Start receiving responses
for try await message in session.responses {
  // Check for new session handles inside your message handling loop
  switch message.payload {
  case let .sessionResumptionUpdate(updateMessage):
    guard let newHandle = updateMessage.newHandle, updateMessage.resumable else {
      continue
    }
    activeSessionHandle = newHandle
    print("SessionResumptionUpdate: handle \(newHandle)")
  // ... handle other LiveServerMessage types ...
  default:
    break
  }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
if let handle = activeSessionHandle {
  session = try await liveModel.connect(
    sessionResumption: SessionResumptionConfig(handle: handle)
  )
}

// Option 2: Resume the session directly on an existing session object
if let handle = activeSessionHandle {
  try await session.resumeSession(
    sessionResumption: SessionResumptionConfig(handle: handle)
  )
}

Kotlin

// Local variable to save the active session handle
var activeSessionHandle: String? = null

// Initialize the session. Passing an empty config requests the server to send SessionResumptionUpdate
var session = liveModel.connect(
    sessionResumption = SessionResumptionConfig()
)

// Start receiving responses
session.receive().collect { message ->
    // Process other received response types...

    // Check for new session handles inside your message handling loop
    if (message is LiveSessionResumptionUpdate) {
        if (message.resumable == true && message.newHandle != null) {
            activeSessionHandle = message.newHandle
            Log.d("TAG", "SessionResumptionUpdate: handle ${message.newHandle}")
        }
    }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
activeSessionHandle?.let { handle ->
    session = liveModel.connect(
        sessionResumption = SessionResumptionConfig(handle = handle)
    )
}

// Option 2: Resume the session directly on an existing session object
activeSessionHandle?.let { handle ->
    session.resumeSession(
        sessionResumption = SessionResumptionConfig(handle = handle)
    )
}

Java

For Java, session resumption is not yet supported. Check back soon!

Web

// Local variable to save the active session handle
let activeSessionHandle = null;

// Initialize the session. Passing an empty object requests the server to send SessionResumptionUpdate
let session = await liveModel.connect({});

// Start receiving responses
for await (const message of session.receive()) {
  // Process other received response types...

  // Check for new session handles inside your message handling loop
  if (message.type === 'sessionResumptionUpdate') {
    if (message.resumable && message.newHandle) {
      activeSessionHandle = message.newHandle;
      console.log(`SessionResumptionUpdate: handle ${activeSessionHandle}`);
    }
  }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
if (activeSessionHandle) {
  session = await liveModel.connect({
    handle: activeSessionHandle
  });
}

// Option 2: Resume the session directly on an existing session object
if (activeSessionHandle) {
  await session.resumeSession({
    handle: activeSessionHandle
  });
}

Dart

// Local variable to save the active session handle
String? _activeSessionHandle;

// Initialize the session. Passing an empty config requests the server to send SessionResumptionUpdate
var _session = await _liveModel.connect(
  sessionResumption: SessionResumptionConfig(),
);

// Start receiving responses
await for (final message in _session.receive()) {
  // Process other received response types...

  // Check for new session handles inside your message handling loop
  if (message is SessionResumptionUpdate &&
      message.resumable != null &&
      message.resumable!) {
    _activeSessionHandle = message.newHandle;
    log('SessionResumptionUpdate: handle ${message.newHandle}');
  }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
if (_activeSessionHandle != null) {
  _session = await _liveModel.connect(
    sessionResumption: SessionResumptionConfig.resume(_activeSessionHandle!),
  );
}

// Option 2: Alternatively, resume the session directly on an existing session object
if (_activeSessionHandle != null) {
  await _session.resumeSession(
    sessionResumption: SessionResumptionConfig.resume(_activeSessionHandle!),
  );
}

Unity

// Local variable to save the active session handle
string activeSessionHandle = null;

// Initialize the session. Passing an empty config requests the server to send SessionResumptionUpdate
var session = await liveModel.ConnectAsync(
    sessionResumption: new SessionResumptionConfig()
);

// Start receiving responses
await foreach (var response in session.ReceiveAsync())
{
  // Process other received response types...

  // Check for new session handles inside your message handling loop
  if (response.Message is LiveSessionResumptionUpdate updateMessage)
  {
    if (updateMessage.Resumable == true && !string.IsNullOrEmpty(updateMessage.NewHandle))
    {
      activeSessionHandle = updateMessage.NewHandle;
      Debug.Log($"SessionResumptionUpdate: handle {activeSessionHandle}");
    }
  }
}

// The following are alternative options to resume a session. Choose only one.

// Option 1: Create and connect a session to resume with the saved handle
if (!string.IsNullOrEmpty(activeSessionHandle)) {
  session = await liveModel.ConnectAsync(
      sessionResumption: new SessionResumptionConfig(activeSessionHandle)
  );
}

// Option 2: Resume the session directly on an existing session object
if (!string.IsNullOrEmpty(activeSessionHandle)) {
  await session.ResumeSessionAsync(
      sessionResumption: new SessionResumptionConfig(activeSessionHandle)
  );
}