Get progress data for the client

I’m new to meteor and i’m really enjoying using the platform.
However i’m stuck! I have no clue if it’s possible.
But what want to do is communicate the progress on the server method download on the client side?
I want show the download progress (0% / 100%).

console.log("Downloading " + (100.0 * cur / len).toFixed(2) + "% ");

Are there any packages or methods to show this?

if (Meteor.isClient) {
    Template.npmtest.events({
        'click button': function () {
            Meteor.call('download', 'https://www.youtube.com/watch?v=bpBOhxwlEYA');
        }
    });
}

if (Meteor.isServer) {
    Meteor.methods({
        download: function (link) {
            var fs = Meteor.npmRequire('fs');
            var youtubedl = Meteor.npmRequire('youtube-dl', ['--format=140']);
            var video = youtubedl(link);
            var cur = 0;
            var len = 0;

            video.on('info', function(info) {
                len = info.size;
            });

            video.on("data", function(chunk) {
                cur += chunk.length;
                console.log("Downloading " + (100.0 * cur / len).toFixed(2) + "% ");
            });

            video.pipe(fs.createWriteStream('myvideo.mp4'));
        }
    });
}

just type progress in atmosphere
we are using this on whole app settinghead:auto-nprogress

That won’t work.

That is for DDP based communications. This is an external web request.

A possible solution would be to just proxy everything through a collection, e.g.:

var Downloads = new Mongo.Collection('downloads');

if(Meteor.isClient){
    Meteor.call('download', function(downloadId){
        Meteor.subscribe('downloadInfo', downloadId);
    });
}

if(Meteor.isServer){
    Meteor.methods({
        'download': function(){
            var downloadId = Downloads.insert({percentage: 0});
            Meteor.defer(function(){
                // download logic
                Downloads.update(downloadId, {$set: {percentage: 0.1}}); // call this when download percentage changes
            });
            return downloadId;
        }
    });

    Meteor.publish('downloadInfo', function(downloadId){
        return Downloads.find(downloadId);
    });
}

My last post was a bit more convoluted that didn’t use a collection (and would therefore be less expensive), but wasn’t sure if it was a good example for a beginner.

1 Like

Thanks for your great response :slight_smile:
I will try it out and post the results on this post.