#cartesia-js websocket client throws error on websocket.connect()

1 messages · Page 1 of 1 (latest)

cobalt shale
#

Trying to implement a POC for websocket connection using @cartesia/cartesia-js client library, ran into the following error:-

##Error##
‼️ No WebSocket implementation available. You should define options.WebSocket.

For example, if you're using node.js, run npm install ws, and then in your code:

import PartySocket from 'partysocket';
import WS from 'ws';

const partysocket = new PartySocket({
host: "127.0.0.1:1999",
room: "test-room",
WebSocket: WS
});

Failed to connect to Cartesia: Error: WebSocket failed to connect.
file:///C:/Users/4%20Way%20Technologies/Downloads/cartesia%20ai/node_modules/@cartesia/cartesia-js/dist/chunk-IQAXBRHU.js:65
throw new Error("Not connected to WebSocket. Call .connect() first.");
^

Error: Not connected to WebSocket. Call .connect() first.
at WebSocket.send (file:///C:/Users/4%20Way%20Technologies/Downloads/cartesia%20ai/node_modules/@cartesia/cartesia-js/dist/chunk-IQAXBRHU.js:65:13)
at file:///C:/Users/4%20Way%20Technologies/Downloads/cartesia%20ai/index9.js:22:28

Node.js v18.18.2

**Code ** that i used :-

import Cartesia from "@cartesia/cartesia-js";

const cartesia = new Cartesia({
apiKey: "xxxxxxxxxxxxxxxxxx",
});

// Initialize the WebSocket. Make sure the output format you specify is supported.
const websocket = cartesia.tts.websocket({
sampleRate: 44100
});

try {
await websocket.connect();
} catch (error) {
console.error(Failed to connect to Cartesia: ${error});
}

// Create a stream.
const response = websocket.send({
model_id: "sonic-english",
voice: {
mode: "id",
id: "a0e99841-438c-4a64-b679-ae501e7d6091",
},
transcript: "Hello, world!"
// The WebSocket sets output_format on your behalf.
});

Could you please help - whats the proper way of implementing a websocket, is there something that i'm missing or doing something wrong?

graceful iron
#

The error is telling you exactly how to fix this.

cobalt shale
#

correct me if i'm wrong but i think cartesia uses partysocket and ws internally, for making socket connections,

`No WebSocket implementation available. You should define options.WebSocket.
For example, if you're using node.js, run npm install ws, and then in your code:
import PartySocket from 'partysocket';
import WS from 'ws';

const partysocket = new PartySocket({
host: "127.0.0.1:1999",
room: "test-room",
WebSocket: WS
});`

Above is the exception thrown by the partysocket itself.
If I use partysocket then , i'll have to handle websocket connections myself but cartesia-js abstracts the websocket implementation

graceful iron
#

just to give it a try, did you run: "npm install ws" ?

#

(im not a cartesia employee, just trying to help)

cobalt shale
#

i did, still not working🥲
(Thanks man, would take all the help i can get)

marble sinew
#

We'll take a look and get back to you tomorrow - thanks for reporting and thanks for the help @graceful iron !

balmy plover
#

hey @cobalt shale could you try upgrading to Node.js version 21? recent node versions added native support for websockets

cobalt shale
#

Hi @balmy plover , i am already running it over nodeV21.7.1

cobalt shale
balmy plover
#

@cobalt shale could you also confirm what cartesia/cartesia-js version you're on?

cobalt shale
#

@balmy plover i have tried both "@cartesia/cartesia-js": "^1.0.2" and "@cartesia/cartesia-js": "^1.0.0-alpha.4"

plain zinc
#

cartesia/cartesia-js is working for me in ANgular

#

only issue i have is trying to play the audio

#

i've set this - player = new WebPlayer.WebPlayer({ bufferDuration: 5000 })

#

then i do the playback here

#

response.on("message", async (message: any) => {

  console.log("Received message:", message)

  await this.player.play(message)

  console.log("Done playing.")

})
#

i do see the response is TEXT - do i need to convert this to an object before passing it into the player?