I’m integrating Yandex SpeechKit for speech-to-text recognition in my application. I’m following the documentation provided by Yandex: https://yandex.cloud/en/docs/speechkit/stt/api/streaming-examples#node_1
The connection is working, but I’m facing an issue where the logs related to the recognition process are not being displayed in the console.
Please help
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const fs = require('fs');
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const apiKey = '**************';
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
// Load Yandex STT service proto file
const packageDefinition = protoLoader.loadSync('../yandex/cloud/ai/stt/v2/stt_service.proto', {
includeDirs: ['node_modules/google-proto-files', '..']
});
const packageObject = grpc.loadPackageDefinition(packageDefinition);
const serviceConstructor = packageObject.yandex.cloud.ai.stt.v2.SttService;
const grpcCredentials = grpc.credentials.createSsl(fs.readFileSync('./roots.pem'));
const service = new serviceConstructor('stt.api.cloud.yandex.net:443', grpcCredentials);
// Set up gRPC metadata
const serviceMetadata = new grpc.Metadata();
serviceMetadata.add('authorization', `Api-Key ${apiKey}`);
// Yandex STT request configuration
const requestConfig = {
config: {
specification: {
languageCode: 'ru-RU',
profanityFilter: true,
model: 'general',
partialResults: true,
audioEncoding: 'LINEAR16_PCM',
sampleRateHertz: 8000 // Ensure it's a number, not a string
},
}
};
let audioInput = [];
let recognizeStream = null;
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
app.get('/', function(req, res) {
res.send('<h1>Hello world!</h1>');
});
wss.on('connection', (ws) => {
console.log('WebSocket connection established');
// Create a writable stream
var wstream = fs.createWriteStream('myBinaryFile');
// Clear the current audioInput
audioInput = [];
// Initiate stream recognizing
recognizeStream = service['StreamingRecognize'](serviceMetadata);
recognizeStream.write(requestConfig)
recognizeStream.on('data', (response) => {
console.log('Start chunk: ');
response.chunks[0].alternatives.forEach((alternative) => {
console.log('Received transcription: ', alternative.text);
ws.send(alternative.text);
process.stdout.write(`Transcription: ${alternative.text}n`);
});
console.log('Is final: ', Boolean(response.chunks[0].final));
console.log('');
});
recognizeStream.on('error', (err) => {
console.error('gRPC Stream Error:', err);
ws.close();
});
recognizeStream.on('end', () => {
console.log('Recognition stream ended');
});
ws.on('close', () => {
console.log('WebSocket connection closed');
wstream.end();
recognizeStream.end();
});
ws.on('message', (message) => {
try {
let data = JSON.parse(message);
if (data.event === "media") {
let b64data = data.media.payload;
let buff = Buffer.from(b64data, 'base64');
recognizeStream.write({ audioContent: buff });
wstream.write(buff);
}
} catch (err) {
console.error('Error processing message:', err);
console.log('Message content:', message);
}
});
ws.send('Hi there, I am a WebSocket server');
});
server.listen(3000, function() {
console.log('listening on *:3000');
});
The problem is not with the authorization, as I’ve tested it with a simple example and it works fine. The issue is specifically with displaying logs
Logs regarding to ws are good
Zhaneiro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.