In Local Environment my application was running smoothly without any issues, but when i deployed it on EC2, I was getting the error querying device -1 error while i was trying to send streaming audio data to the backend via a WebSocket.
useEffect(() => {
socketRef.current = new WebSocket('ws://3.6.39.130:8000/ws');
socketRef.current.onopen = () => {
console.log('WebSocket connected');
};
socketRef.current.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Received transcription:', data.transcript);
// Update UI or state with transcription data
};
socketRef.current.onclose = () => {
console.log('WebSocket connection closed');
};
socketRef.current.onerror = (error) => {
console.error('WebSocket error:', error);
};
return () => {
// Clean up WebSocket connection on component unmount
if (socketRef.current) {
socketRef.current.close();
}
};
}, []);
const startRecording = () => {
navigator.mediaDevices.getUserMedia({ audio: true })
.then((stream) => {
// Start WebSocket communication
socketRef.current.send(JSON.stringify({ action: 'start' }));
// Process audio stream chunks and send to WebSocket
const mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
socketRef.current.send(event.data);
}
};
mediaRecorder.start();
})
.catch((error) => {
console.error('Error accessing microphone:', error);
});
};
const stopRecording = () => {
// Stop MediaRecorder and WebSocket communication
if (mediaRecorder.state === 'recording') {
mediaRecorder.stop();
}
socketRef.current.send(JSON.stringify({ action: 'stop' }));
};
I have used mediaRecorder to get audio from user’s Browser. Also tried to get the necessary permissions from the user to access the microphone.
//fastapi endpoint
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
stop_event = asyncio.Event()
try:
while True:
data = await websocket.receive_json()
if data['action'] == 'start':
print("Starting transcription...")
stop_event.clear()
asyncio.create_task(transcribe(stop_event))
elif data['action'] == 'stop':
print("Stopping transcription...")
stop_event.set()
except WebSocketDisconnect:
print("WebSocket disconnected")
stop_event.set()
New contributor
Parimal Kolhe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.