Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js client-side progress indicator for server operations

My node.js server will perform certain operations to uploaded files and I would like to reflect the progress in near real-time back to the client. This operation takes about 30 seconds - 1 minute to complete.

Ideally, upon file upload, the client will be able to observe the progress of the server operation on the file.

I could either perform a regular poll, long poll, or connect to the server via websockets, but all these seem to require rather complicated set up for a 30sec-1min progress indicator.

Are there better methods available?

like image 356
Ming C Avatar asked Apr 12 '14 17:04

Ming C


1 Answers

I would do this with socket.io. Better methods may be available, but socket.io is relatively easy to configure.

Are you using Express.js? If so make sure you look at the section specific to Express for details on configuration. Once configured you'll want to do the following:

On the server, you will want to emit a new status

socket.emit('status','aok!');

On the client, you will want to listen for a new status from the server

socket.on('status', function (msg) {
    console.log(msg);
    //Put code here to update the page!
});

Something you may want to consider is adjusting the heartbeat time. Basically the server pings the client every so often to check if they are still connected. I believe by default this happens every 25 seconds with a timeout occurring with no response after 60 seconds. You may want to lengthen the time between heartbeats, or even disable them entirely.

like image 140
bobbyg603 Avatar answered Nov 07 '22 13:11

bobbyg603