Eesa Munir
Eesa Munir

Reputation: 83

Sending an image/video using sockets and node

My application requires a section for users to communicate each other. For this, I am using socket-io. For sending text(as strings). I use utf-8, which works perfectly.

However, when sending an image or a video on a socket, how do I approach this? Would I turn the image or the video into binary format, and send that on the socket?

Upvotes: 0

Views: 731

Answers (1)

Conan
Conan

Reputation: 319

Yes there is an example about how to send your files with socket.io :

var fileReader = new FileReader(), 
    slice = file.slice(0, 100000); 

fileReader.readAsArrayBuffer(slice); 
fileReader.onload = (evt) => {
    var arrayBuffer = fileReader.result; 
    socket.emit('slice upload', { 
        name: file.name, 
        type: file.type, 
        size: file.size, 
        data: arrayBuffer 
    }); 
}

there is a full tutorial with example about send file with socket.io and receive it in the server nodeJs follow this

Upvotes: 1

Related Questions