Search code examples
node.jswebsocketgoogle-speech-api

How to send websocket binary messages as stream to Google Speech API?


I'm trying to send the audio stream of a websocket connection to the Google Speech API. The websocket sends binary messages in 20ms increments. That it sends it in increments leads me to believe that I will somehow have to read and write the data to a local file temporarily to avoid killing the connection to Google. However, this is not ideal.

Is there a way to directly pipe the websocket stream into recognizeStream?

Google streamingRecognize example from the docs:

const request = {
   config: {
      encoding: encoding,
      sampleRate: sampleRate
   }
};

const recognizeStream = speech.createRecognizeStream(request)
  .on('error', console.error)
  .on('data', (data) => process.stdout.write(data.results));

record.start({
  sampleRate: sampleRate,
  threshold: 0
}).pipe(recognizeStream);

Websocket connection:

var HttpDispatcher = require('httpdispatcher');
var dispatcher     = new HttpDispatcher();
var WebSocketServer = require('websocket').server;


var server = http.createServer(handleRequest);

var wsServer = new WebSocketServer({
    httpServer: server,
    autoAcceptConnections: true,

});

function handleRequest(request, response){
    try {
        //log the request on console
        console.log(request.url);
        //Dispatch
        dispatcher.dispatch(request, response);
    } catch(err) {
        console.log(err);
    }
}


wsServer.on('connect', function(connection) {
    console.log((new Date()) + ' Connection accepted' + ' - Protocol Version ' + connection.webSocketVersion);
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log(message.utf8Data);
        }

        else if (message.type === 'binary') {

          //Send to Google Speech API by passing into recognizeStream

        }
    });

    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });


});

Solution

  • This is actually quite simple. So simple that I feel a little sheepish for not seeing it. In accordance to exactly how the code was written in the OP this works perfectly:

    else if (message.type === 'binary') {
    
      //Send to Google Speech API by passing into recognizeStream
      recognizeStream.write(message.binaryData)
    
    }