RetellAI / retell-client-js-sdk

10 stars 10 forks source link

The socket connection closes after some time #6

Open VladislavYakonyuk opened 7 months ago

VladislavYakonyuk commented 7 months ago

My socket connection closes after some time, for a reason unknown to me. I used your client-js-sdk and the example in the demo repository

const webClient = new RetellWebClient();

export default function Home() {
    const [ callActive, setCallActive ] = useState<boolean>(false);

    useEffect(() => {
        // Setup event listeners
        webClient.on("conversationStarted", () => {
            console.log("conversationStarted");
        });

        webClient.on("audio", (audio: Uint8Array) => {
            console.log("There is audio");
        });

        webClient.on("conversationEnded", ({ code, reason }) => {
            console.log("Closed with code:", code, ", reason:", reason);
            setCallActive(false); // Update button to "Start" when conversation ends
        });

        webClient.on("error", (error) => {
            console.error("An error occurred:", error);
            setCallActive(false); // Update button to "Start" in case of error
        });

        webClient.on("update", (update) => {
            // Print live transcript as needed
            console.log("update", update);
        });
    }, []);

    const toggleConversation = async () => {
        if (callActive) {
            webClient.stopConversation();
        } else {
            const registerCallResponse = await registerCall();
            if (registerCallResponse.call_id) {
                webClient
                    .startConversation({
                        callId: registerCallResponse.call_id,
                        sampleRate: 24000,
                        enableUpdate: true,
                    })
                    .catch(console.error);
                setCallActive(true); // Update button to "Stop" when conversation starts
            }
        }
    };

    const registerCall = async (): Promise<{ call_id: string }> => {

        const res = await axios.post<{ call_id: string }>("http://my-server.com/api/v1/calls/register-call/", {
            audio_encoding: "s16le",
            audio_websocket_protocol: "web",
            sample_rate: 24000
        });

        return res.data;
   }
   ...
}
image image
tarunkumarmetaforms commented 3 months ago

are you still facing the same issue