I face a particular challenge that I can not solve. I have a remote URL (ex. https://www.somesite.com/file.mp3) and I would like it to be streamed to a WebSocket.
Some may say I can just stream from the request, but there are particular settings on the createReadStream that need to be set.
Another challenge is that the WebSocket only accepts .wav files, so I must convert the .mp3 to a .wav somewhere on the line. My main goal is speed and performance, and to conserve time and resources is crucial.
const ws = new WebSocket('wss://ws.somesite.com/websocket');
var readStream = fs.createReadStream('./audio_file.wav', {
flags: 'r',
highWaterMark: 4096
});
readStream.on('data', async chunk => {
ws.send(chunk, {
binary: true,
mask: true
})
})
Optimally, in a perfect world I could do something like this (yes, I know this code does not work and is an example/psuedocode).
const ws = new WebSocket('wss://ws.anothersite.com/websocket');
// Made source a remote URL.
var readStream = fs.createReadStream('https://www.somesite.com/file.mp3', {
flags: 'r',
highWaterMark: 4096,
convert: 'wav' // Added this.
});
readStream.on('data', async chunk => {
ws.send(chunk, {
binary: true,
mask: true
})
})
Thank you for ANY input/help you can make!