callback to handle completion of pipe


I am using the following node.js code to download documents from some url and save it in the disk. I want to be informed about when the document is downloaded. i have not seen any callback with pipe.Or, Is there any 'end' event that can be captured on completion of download ?

Problem courtesy of: user644745


Streams are EventEmitters so you can listen to certain events. As you said there is a finish event for request (previously end).

 var stream = request(...).pipe(...);
 stream.on('finish', function () { ... });

For more information about which events are available you can check the stream documentation page.

Solution courtesy of: Pickels


Based nodejs document,, it should handle writableStream's finish event.

var writable = getWriteable();
var readable = getReadable();
writable.on('finish', function(){ ... });
Discussion courtesy of: smartwjw

I found an a bit different solution of my problem regarding this context. Thought worth sharing.

Most of the example create readStreams from file. But in my case readStream has to be created from JSON string coming from a message pool.

var jsonStream = through2.obj(function(chunk, encoding, callback) {
                    this.push(JSON.stringify(chunk, null, 4) + '\n');
// message.vale --> valeu/text to write in write.txt 
var writeStream = sftp.createWriteStream("/path/to/write/write.txt");

//"close" event didn't work for me!
writeStream.on( 'close', function () {
    console.log( "- done!" );

//"finish" event didn't work for me either!
writeStream.on( 'close', function () {
    console.log( "- done!"

// finally this worked for me!
jsonStream.on('data', function(data) {
    var toString =;
    console.log('type of data:', toString);
    console.log( "- file transferred" );

jsonStream.pipe( writeStream );
Discussion courtesy of: johnshumon

Code snippet for piping content from web via http(s) to filesystem. As @starbeamrainbowlabs noticed event finish does job

var tmpFile = "/tmp/somefilename.doc";

var ws = fs.createWriteStream(tmpFile);
ws.on('finish', function() {
  // pipe done here, do something with file

var client = url.slice(0, 5) === 'https' ? https : http;
client.get(url, function(response) {
  return response.pipe(ws);
Discussion courtesy of: ruX

This recipe can be found in it's original form on Stack Over Flow.