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?