Closed jzombie closed 2 years ago
I assume the issue is related to the WebSocket include in this file. Perhaps it could determine if the browser's WebSocket API is available, and use it instead. I might can make a PR later this week. Using it for a hackathon project.
https://github.com/deepgram/deepgram-node-sdk/blob/main/src/transcription/liveTranscription.ts
Are you attempting to use the Deepgram Node SDK on the frontend? That's not going to work...
You can open a socket to Deepgram not using the SDK and the deepgramSocket, but instead just using the browser websocket api. Here's an example:
https://github.com/deepgram-devs/browser-mic-streaming/blob/main/index.html#L17
That github repo goes along with the tutorial at https://developers.deepgram.com/blog/2021/11/live-transcription-mic-browser/
Is that helpful to you?
Yep, absolutely, Thanks for the help here.
A quick suggestion, this package's README might should be updated to not include the DOM example w/ navigator.mediaDevices.getUserMedia if it's only intended to be used in Node.js (as getUserMedia / MediaStream aren't available in Node w/o polyfills).
https://github.com/deepgram/deepgram-node-sdk#transcribe-audio-in-real-time
Really good point @jzombie. We do have on the roadmap a "browserified" version that would be compatible, but we should update that example.
Quick update @jzombie, the latest version of the SDK should allow you to work in the browser.
After you npm install @deepgram/sdk
, you should be able to access the Deepgram object via @deepgram/sdk/browser
I haven't given an exhaustive code sample below (as in, I'm not showing how to capture the microphone,) but the code below should provide a good starting point.
import { Deepgram } from '@deepgram/sdk/browser';
const deepgram = new Deepgram('DEEPGRAM_API_KEY');
const deepgramSocket = deepgram.transcription.live({ punctuate: true });
deepgramSocket.onopen = () => {
if (microphone.state !== 'recording') {
microphone.addEventListener('dataavailable', async (event) => {
if (event.data.size > 0 && socket.readyState === 1) {
deepgramSocket.send(event.data);
}
});
microphone.start(200);
}
};
deepgramSocket.onmessage = (message) => {
const received = JSON.parse(message.data);
const transcript = received.channel.alternatives[0].transcript;
if (transcript && received.is_final) {
console.log(transcript);
}
};
deepgramSocket.onclose = () => {
console.log('Connection closed.');
};
@MichaelJolley
Thanks for sending this. Have a good weekend.
What is the current behavior?
Followed example for Transcribe Audio in Real-Time and received "Uncaught (in promise) Error: ws does not work in the browser. Browser clients must use the native WebSocket object"
Steps to reproduce
Run this in Chrome:
Expected behavior
Should connect to WebSocket and start transcribing.
Please tell us about your environment
Chrome v99
Mac Monterey / JavaScript / React / Chrome
Other information