'Send a file from NodeJS socketio server to Python socketio client

I have a Frontend application sending file over HTTP to Nodejs Backend. The Nodejs application acts as a socketio server and needs to send the file to a python socketio client. I have used multer to parse the HTTP request. Here are the nodejs and python code I have written so far (only pasted required parts)

// NodeJS server code I have written so far (could be completely wrong)

// req.file contains the file parsed from multer
    var socket = req.app.get('socket');
    let readStream = createReadStream(req.file.path);
    readStream.on('ready', () => {
        socket.emit('image', readStream, data => console.log(data));
        readStream.pipe(createWriteStream(req.file.originalname));
    });
# Python socketio client
@sio.event
async def image(data):
    try:
        print(data)
        # want to save data preferably in a local file or a buffer is also fine
    except:
        return 'NOT OK'
    finally:
        return 'OK'

I am facing issues on both fronts, for both of which I have almost no idea how to procede:

  • sending image from nodejs (thought of using fs.createReadStream but don't know how to use the stream over a socketio event)
  • receiving image on python and storing it locally.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source