How do i properly send stream to a child process. I am trying to stream audio from client to server using this method
I am getting the audio stream from client
const ss = require('socket.io-stream');
const socketIo = require('socket.io');
server = http.createServer(app);
io = socketIo(server);
const child_process = childProcess.fork('./child_process.js');
io.on('connect', (client) => {
// when the client sends 'stream-transcribe' events
// when using audio streaming
ss(client).on('stream-transcribe', function(stream, data) {
var combine_data = {
"data": data,
"stream": stream,
"event":"new_stream"
}
child_process.send(combine_data)
}
}
this call
child_process.send(combine_data)
throws the error
node:internal/child_process/serialization:127 const string = JSONStringify(message) + '\n'; ^
TypeError: Converting circular structure to JSON --> starting at object with constructor 'Namespace' | property 'server' -> object with constructor 'Server' | property 'nsps' -> object with constructor 'Object' --- property '/' closes the circle at stringify () at writeChannelMessage (node:internal/child_process/serialization:127:20) at ChildProcess.target._send (node:internal/child_process:837:17) at ChildProcess.target.send (node:internal/child_process:737:19) at Socket. (/Volumes/GoogleDrive/My Drive/app/server.js:236:40) at Socket.onstream (/Volumes/GoogleDrive/My Drive/app/node_modules/socket.io-stream/lib/socket.js:184:14) at Socket.emit (node:events:526:28) at Socket. (/Volumes/GoogleDrive/My Drive/app/node_modules/component-bind/index.js:21:15) at Socket.emit (node:events:526:28) at /Volumes/GoogleDrive/My Drive/app/node_modules/socket.io/lib/socket.js:528:12
Node.js v17.5.0
what is causing the error and how can I send the stream to child_process ?
Any data you send using child_process.send() will be converted to a string using JSON.stringify(). The data you try to send is too complex to be converted to a string. In particular, it uses circular references. You can easily verify this using console.log() or a debugger.
You can try to send only data that can be converted to a string. You probably should not try to send stream at all and only send the relevant fields of data.
Otherwise, you can probably pipe() the stream to stdin of the child process.
Based on the documentation:
ss(client).on('stream-transcribe', function(stream, data) {
stream.pipe(child_process.stdin);
});
In the child process you can read the data from stdout, which is a readable stream.
child_process.stdout.on('data', (chunk) => {
// do something with `chunk`
});