From 3cc7abf052111711bde5b040ea4ad129a8aa6caa Mon Sep 17 00:00:00 2001 From: Marcel Thomas Date: Tue, 7 May 2024 14:04:38 +0100 Subject: [PATCH] Update index.ts --- .../node-express/src/index.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/examples/text-to-speech-websockets/node-express/src/index.ts b/examples/text-to-speech-websockets/node-express/src/index.ts index 106909c..3d9d90c 100644 --- a/examples/text-to-speech-websockets/node-express/src/index.ts +++ b/examples/text-to-speech-websockets/node-express/src/index.ts @@ -39,17 +39,6 @@ io.on('connection', (socket) => { elevenlabsSocket.send(JSON.stringify(initialMessage)); }; - elevenlabsSocket.onmessage = (event: any) => { - const response = JSON.parse(event.data); - if (response.error) { - socket.emit('error', response); - } - - if (response.audio) { - socket.emit('audio', response.audio); - } - }; - socket.on('message', (data) => { const message = JSON.parse(data) as { text: string; isFinal?: boolean }; const textMessage = { @@ -60,6 +49,18 @@ io.on('connection', (socket) => { elevenlabsSocket.send(JSON.stringify(textMessage)); }); + + elevenlabsSocket.onmessage = (event: any) => { + const response = JSON.parse(event.data); + + if (response.error) { + socket.emit('error', response); + } + + if (response.audio) { + socket.emit('audio', response.audio); + } + }; }); server.listen(PORT, () => {