Discussions

Ask a Question
Back to all

HeyGen > LiveAvatar Migration

Context:

I recently began working on migrating my organization's custom HeyGen Avatar into LiveAvatar. I have come to the step where I am changing the code that hosts connections to our Avatar, and am encountering road bumps that documentation has not clearly addressed.

Questions:

  1. After using the code below to successfully establish a connection (api.js), do I need to create a new reference as shown in index.js via the new LiveAvatarSession? I see this is in our legacy code, and I am confused because I thought I had already established the session in the first step.

    router.get('/token', (req, res) => {
    	const liveAvatarUrl = url.parse(`${liveAvatarUrl}/v1/sessions/token`)
      const requestBody = JSON.stringify({
        mode: 'LITE',
        avatar_id: avatarId,
        is_sandbox: false,
        avatar_persona: {
          voice_id: voiceId,
          language: 'en'
        },
        interactivity_type: 'CONVERSATIONAL'
      });
    
      const options = {
        hostname: parsedUrl.hostname,
        path: parsedUrl.path,
        port: parsedUrl.port || 443,
        method: "POST",
        headers: {
          accept: 'application/json',
          'content-type': 'application/json',
          'X-API-KEY': liveAvatarKey,
          'Content-Length': Buffer.byteLength(requestBody)
        }
      };
      
    
    import { LiveAvatarSession } from '@heygen/liveavatar-web-sdk'
    
    const response = await fetch(`${HOST_URL}/api/token`);
    sessionId = response.data.session_Id;
    sessionToken = response.data.sessionToken;
    
    const userConfig = {
      voiceChat: true,
    }
    
    liveAvatarSession = new LiveAvatarSession(sessionToken, userConfig);
    
  2. How do I use streaming events with the new LiveAvatar? It appears that in the legacy code, we are using session.start() and then defining listeners, is that behavior the same? Reason I ask is because I am able to create a connection and see that in the web-developer tools, but it gets hung up and never begins the session. The snippet below shows how it was done previously, I would like to know how I need to adapt it for the LiveAvatart SDK rather than the previous implementation.

import StreamingAvatar, { AvatarQuality, StreamingEvents, VoiceEmotion } from '@heygen/streaming-avatar'

liveAvatarSession = new LiveAvatarSession(sessionId, sessionToken, userConfig)
liveAvatarSession.on('avatar_start_talking', () => { // How do I do this with LiveAvatar rather than HeyGen?
	console.log('Avatar started talking' 
});