Node.js

Create the WebSocket

First, handle the connection event so that you can report when your webhook server is online and ready to receive the call audio:

expressWs.getWss().on('connection', function (ws) {
  console.log('Websocket connection is open');
});

When writing audio to a Voice API WebSocket, the audio is expected in a specific format. To do this, you will need a function that separates the binary audio data into arrays of the correct size:

function chunkArray(array, chunkSize) {
    var chunkedArray = [];
    for (var i = 0; i < array.length; i += chunkSize)
        chunkedArray.push(array.slice(i, i + chunkSize));
    return chunkedArray;
}

Then, create a route handler for the /socket route. When the WebSocket is connected this route will get called:

app.ws('/socket', (ws, req) => {
    const wav = new WaveFile(fs.readFileSync("./sound.wav"));
    wav.toSampleRate(16000);
    wav.toBitDepth("16");

    const samples = chunkArray(wav.getSamples()[0], 320);
    for (var index = 0; index < samples.length; ++index) {
        ws.send(Uint16Array.from(samples[index]).buffer);
    }
})

The route loads an audio file from disk, you can download the same one from GitHub, uses the WaveFile library to change the sample rate and bit depth for the Voice API. Next it gets the audio samples from the first channel of audio, and uses the function from earlier to change the size of the binary audio data array. Finally the binary audio data is iterated over and sent to the call via the WebSocket with the send() function.

NOTE: Vonage will only buffer 1024 messages which should be enough for around 20 seconds of audio, if your file is longer than this you should implement a delay of 18-19ms between each message.