Socket.io emits progress check

I use Socket.io to download files. It works great with the following scheme:

  • Client: read the file from the input and encode it to Base64 and compile it
  • Client: fire the Socket.io "upload" event, including the compressed file as part of the data
  • Server: listen to the "loading" of the event, unzip and decode the file and save it

The problem arises for large files: I do not see the progress of the data sent using the emit client (for example, I do this with XHR).

To solve these problems, I have to track (check) the progress of the emit event. How can i do this?


I would like to hear how the download progresses only on the CLIENT SIDE side.


Thanks to bnuhero, socketio-file-upload makes it possible to listen to progress on the server side and send messages to the client side, when it can be said that every 5% or 20% is downloaded. Thus, this means sending 20 or 5 messages respectively to a file. I would like to listen to progress on the client side.


It seems that there is no way to check progress on the client side with Socket.io exposed. Io-stream solves this problem.

+4
source share
2 answers

customer:

var chunk_size = 100;
var compressed_data = 'some-long-string';
var reg = new RegExp('/.{1,'+chunk_size+'}/g');
var parts = compressed_data .match(reg);
var l = parts.length -1;

client.socket.emit('data', parts.pop());

client.socket.on('received', function () {
    client.socket.emit('data', [l - parts.lengt, parts.pop()]);
});

Server:

sockets.on('connection', function (socket) {
  var parts = [];

  socket.on('data', function (data) {
     if (data[1] === undefined) {
         fs.writeFile(parts.join(''), callback...);
     } else {
         parts[data[0]] = data[1];
         socket.emit('received');
     }
  });


});

, , "" , , ""

, , , ,

EDIT:

EDIT2:

,

, , , , , , 2 , , , , .

- , ( 1 , 1 , 0 100 .

https://github.com/nkzawa/socket.io-stream

- , github

:

var io = require('socket.io-client');
var ss = require('socket.io-stream');

var socket = io.connect('http://example.com/user');
var stream = ss.createStream();
var filename = 'profile.jpg';
var through = require('through');

var compressed_data = 'some-long-string';
var l = compressed_data .length;
var total_progress = 0;

//pass the stream trough throug, giving feedback for each chunk passed
var tr = through(function (chunk) {
    total_progress += chunk.toString().length;
    client.socket.emit('progress', l, total_progress);
    this.queue(chunk)
}, function () {
    client.socket.emit('progress', l, l);
})

//use the streaming version of socket.io
ss(socket).emit('profile-image', stream, {name: filename});

//get a stream for the compressed_data
//filter it trough tr for the progress indication
//and pass it to the socket stream
fs.createReadStream(compressed_data ).pipe(tr).pipe(stream);

, , ,

+6

, , socket.io-stream 100 , socket.io

-2

All Articles