I have been trying to use socketio to stream files from nodejs server to the python socketio client. However I don't have a proper idea how to stream image. Currently I am just using readFile in Nodejs. This is what I have written so far to read the file and send it as an whole:
var socket = req.app.get('socket');
readFile(req.file.path, 'utf-8', (err, data) => {
socket.emit('image', { buffer: data, metadata: req.file }, (response) => {
console.log(response);
});
});
On python socketio client's side, I want to save the file to a folder assets and I have written this code:
@sio.event
async def image(data):
try:
filehandler = open('assets/' + data['metadata']['filename'] + '.jpeg', 'w')
filehandler.write(data['buffer'])
filehandler.close()
except:
return 'NOT OK'
finally:
return 'OK'
However, the final file written is not readable as an image and I am not able to open it. Please correct my code as required for this to work properly.
Moreover, I want to stream the file from Nodejs instead of reading it whole and then sending it.
I would really appreciate it if I got code for both methods but at least need the code for streaming the file.
Thanks!!