Discussions

Ask a Question
Back to All

createStartAvatar Hangs and Prevents Further Execution

Hello everyone,

I’m encountering a problem when initializing an avatar session. The execution sometimes gets stuck at line 26 (the call to createStartAvatar) and never proceeds to the subsequent code (line 56).

Below is a relevant excerpt of the code:

import StreamingAvatar, { AvatarQuality, StreamingEvents, TaskType } from "@heygen/streaming-avatar";
import { fetchAccessToken, sendAudioToServer } from "./api";
import ...
import ...

export class AvatarSession {
  private avatar: StreamingAvatar | null = null;
  private sessionData: any = null;
  private currentStep = 0;
  private welcomeText = "";
  private speechResolve: (() => void) | null = null;
  private isTerminated = false;
  private videoElement: HTMLVideoElement;
  private agence: string;

  constructor(videoElement: HTMLVideoElement, agence: string) {
    this.videoElement = videoElement;
    this.agence = agence;
  }

  public async initializeSession(): Promise<void> {
    this.isTerminated = false;
    try {
      const token = await fetchAccessToken(); // The token is successfully generated
      this.avatar = new StreamingAvatar({ token });
      // The following call sometimes hangs and never proceeds
      this.sessionData = await this.avatar.createStartAvatar({
        quality: AvatarQuality.High,
        avatarName: "Dexter_Lawyer_Sitting_public",
        language: "English",
      });
      this.bindEvents();
    } catch (error) {
      console.error("Failed to initialize avatar session:", error);
    }
  }

  private bindEvents(): void {
    if (!this.avatar) return;
    this.avatar.on(StreamingEvents.STREAM_READY, this.handleStreamReady);
    this.avatar.on(StreamingEvents.STREAM_DISCONNECTED, this.handleStreamDisconnected);
    this.avatar.on(StreamingEvents.AVATAR_STOP_TALKING, this.handleSpeechComplete);
  }

  private handleStreamReady = async (event: any): Promise<void> => {
    // Code that should run after line 26 (line 56 and onwards)
    // ...
  };

  // ...
}

Below are screenshots from the Network tab showing some requests stuck or not completing:


  • We can see that streaming.create_token returns a 200 OK status, so the token is indeed generated.
  • However, after that, the request or connection associated with createStartAvatar sometimes remains blocked (or pending).
  • As a result, the code never reaches the subsequent lines (for example, the handleStreamReady function is never triggered).

Has anyone encountered this issue or have any suggestions?

  • I’m wondering if a specific parameter (avatarName, quality, language) could be causing the hang,
  • Or if an internal event (e.g., STREAM_READY) is not firing,
  • Or if it’s a networking / WebSocket configuration issue.

Any help or insight would be greatly appreciated! Thank you in advance for your time.