Discussions

Ask a Question
Back to All

Erroe WS connetion StartvoiceChat

There is my log in the console :

Starting avatar initialization...

Creating avatar session...
Stream ready event: CustomEvent {isTrusted: false, detail: MediaStream, type: 'stream_ready', target: EventTarget, currentTarget: EventTarget, …}isTrusted: falsebubbles: falsecancelBubble: falsecancelable: falsecomposed: falsecurrentTarget: EventTarget {}defaultPrevented: falsedetail: MediaStream {id: '24baa1ae-d9f9-4173-a463-5dbafd01377b', active: true, onaddtrack: null, onremovetrack: null, onactive: null, …}eventPhase: 0returnValue: truesrcElement: EventTarget {}target: EventTarget {}timeStamp: 138741.5type: "stream_ready"[[Prototype]]: CustomEvent

WebSocket connection to 'wss://api.heygen.com/v1/ws/streaming.chat?session_id=8dc12bc6-9b53-11ef-90e6-caf3d469ab2d&session_token=eyJ0b2tlbiI6ICIxMmZiNDNhYmExY2M0OWU1ODBkNWRkNjI1NDMzYmM1ZiIsICJ0b2tlbl90eXBlIjogInNhX2Zyb21fcmVndWxhciIsICJjcmVhdGVkX2F0IjogMTczMDc5Njg2M30=&silence_response=true&stt_language=fr-FR' failed:

Event {isTrusted: true, type: 'error', target: WebSocket, currentTarget: WebSocket, eventPhase: 2, …}

  1. isTrusted: true
  2. bubbles: false
    cancelBubble: false
  3. cancelable: false
  4. composed: false
  5. currentTarget:

WebSocket {url: 'wss://api.heygen.com/v1/ws/streaming.chat?session_…NjkwOH0=&silence_response=true&sttlanguage=fr-FR', readyState: 3, bufferedAmount: 0, onopen: null, onerror: null, …} 7. defaultPrevented: false 8. eventPhase: 0 9. returnValue: true 10. srcElement: WebSocket {url: 'wss://api.heygen.com/v1/ws/streaming.chat?session…NjkwOH0=&silence_response=true&sttlanguage=fr-FR', readyState: 3, bufferedAmount: 0, onopen: null, onerror: null, …}

  1. target: WebSocket {url: 'wss://api.heygen.com/v1/ws/streaming.chat?session…NjkwOH0=&silence_response=true&stt_language=fr-FR', readyState: 3, bufferedAmount: 0, onopen: null, onerror: null, …}
  2. timeStamp: 8946.900000035763
  3. type: "error"
    HeygenDialog.tsx:69 Voice initialization error:

I dont understand the sesssion is created fine no probme, but when i call

                    await avatar.startVoiceChat();

I have a websocket connectionn error : wss://api.heygen.com/v1/ws/streaming.chat?session_id=b2170895-9b58-11ef-9963-5631dcfa9e71&session_token=eyJ0b2tlbiI6ICIxOWM5MjBkMzM1YTY0MjZjYjg0MDcyZjg0NTg1YzQ1MyIsICJ0b2tlbl90eXBlIjogInNhX2Zyb21fcmVndWxhciIsICJjcmVhdGVkX2F0IjogMTczMDc5OTA3MX0=&silence_response=false&stt_language=fr-FR

There is my code


const initializeAvatar = async () => {
    try {
        setIsProcessing(true);
        console.log("Starting avatar initialization...");

        // 1. Token et avatar
        const tokenResponse = await fetch("https://api.heygen.com/v1/streaming.create_token", {
            method: "POST",
            headers: {
                "x-api-key": API_KEY,
                "Content-Type": "application/json"
            },
        });
        const {data: {token}} = await tokenResponse.json();

        const avatar = new StreamingAvatar({
            token,
            basePath: "https://api.heygen.com"
        });

        // 2. Configuration audio d'abord
        audioStreamRef.current = await navigator.mediaDevices.getUserMedia({
            audio: {
                echoCancellation: true,
                noiseSuppression: true,
                autoGainControl: true,
                channelCount: 1,
                sampleRate: 16000
            }
        });

        // 3. Événements
        avatar.on(StreamingEvents.STREAM_READY, async (event) => {
            console.log('Stream ready:', event.detail);
            if (videoRef.current) {
                videoRef.current.srcObject = event.detail;
                try {
                    await videoRef.current.play();
                    setIsReady(true);
                    if (!avatarRef.current || !sessionInfo.session_id) {
                        console.error("WebSocket or session is not ready.");
                        return;
                    }
                    if (!audioStreamRef.current || audioStreamRef.current.getTracks().length === 0) {
                        console.error("No audio tracks available.");
                        return;
                    }

                    // 5. Démarrage du chat vocal après le stream
                    await avatar.startVoiceChat();
                } catch (error) {

                    console.error('Error starting video or voice chat:', error);
                }
            }
        });


        avatar.on(StreamingEvents.USER_START, () => setIsUserTalking(true));
        avatar.on(StreamingEvents.USER_STOP, () => setIsUserTalking(false));

        // 4. Création de la session en dernier
        const sessionInfo = await avatar.createStartAvatar({
            quality: AvatarQuality.High,
            avatarName: "default",
            voice: {
                voiceId: "6e989df1661149a09fb9cf87ea460dee",
                emotion: VoiceEmotion.FRIENDLY,
                rate: 1.0
            },
            language: "fr-FR"
        });

        console.log("Session created:", sessionInfo);
        avatarRef.current = avatar;

    } catch (error) {
        console.error('Initialization error:', error);
        toast.error("Impossible d'initialiser l'avatar");

        if (audioStreamRef.current) {
            audioStreamRef.current.getTracks().forEach(track => track.stop());
            audioStreamRef.current = null;
        }
    } finally {
        setIsProcessing(false);
    }
};