I am struggling to implement a socket.io progress function. I want to emit from the server the upload progress. Using https://github.com/aheckmann/gridfs-stream to insert the image into a database, this should show the client the progress of the image streaming the bytes until the image is finally finished I would also like to emit this progress to the connected user in the chat through sockets.
So here is what I have.
var conn = mongoose.createConnection('mongodb://myserver.io/MyApp');
var form = new multiparty.Form();
var size = '';
var fileName = '';
var fstream;
var writeStream,
readStream,
buffer = "";
//more logical stream code
conn.once('open', function () {
var gfs = Grid(conn.db, mongoose.mongo);
app.post('/upload', function(req,res){
req.pipe(req.busboy, { end: false });
req.busboy.on('file', function (fieldname, file, filename) {
console.log(filename);
var fstream = gfs.createWriteStream({ filename: filename }); // Cloud
// fstream = fs.createWriteStream(__dirname + '/public/img' + filename); // Local
file.pipe(fstream);
fstream.on('progress', function (size) {
console.log('Size'+size);
io.sockets.on('connection', function (socket) {
var progress = size;
socket.broadcast.emit('new image', progress);
});
});
});
});
});
The result of the console.log for size
is just the bytes in chunks example
Size33178
Size56538
Size72030
Size74950
Size130914
Size144852
POST /upload 302 2452ms - 35b
At least I think those are bytes, I am not sure how I can turn those numbers into a percent, is there a calculation for this, or another method. Any help would be great!