Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js- writing data to the writable stream

Tags:

stream

node.js

In my node application im writing data to the file using write method in the createWriteStream method.Now i need to find whether the write for the particular stream is complete or not.How can i find that.

var stream = fs.createWriteStream('myFile.txt', {flags: 'a'});
var result = stream.write(data);

writeToStream();
function writeToStream() {
  var result = stream.write(data + '\n');
  if (!result) {
    stream.once('drain',writeToStream());
  }
}

I need to call other method for every time when write completes.How can i do this.

like image 555
sachin Avatar asked Sep 27 '13 13:09

sachin


People also ask

What is the correct way to pipe a readable stream and a writable stream node?

To consume a readable stream, we can use the pipe / unpipe methods, or the read / unshift / resume methods. To consume a writable stream, we can make it the destination of pipe / unpipe , or just write to it with the write method and call the end method when we're done.

Can a output of readable stream be an input to the writable stream?

Readable stream is used for read operation and its output can be input to a writable stream.

What is writable stream API?

The WritableStream interface of the Streams API provides a standard abstraction for writing streaming data to a destination, known as a sink. This object comes with built-in backpressure and queuing. WritableStream is a transferable object.

What does .pipe do in node?

pipe was added in v0. 9.4 of Node. js and its purpose is to attach a writeable stream to a readable stream allowing to pass the readable stream data to the writeable stream.


2 Answers

From the node.js WritableStream.write(...) documentation you can give the "write" method a callback that is called when the written data is flushed:

var stream = fs.createWriteStream('myFile.txt', {flags: 'a'});
var data = "Hello, World!\n";
stream.write(data, function() {
  // Now the data has been written.
});

Note that you probably don't need to actually wait for each call to "write" to complete before queueing the next call. Even if the "write" method returns false you can still call subsequent writes and node will buffer the pending write requests into memory.

like image 63
maerics Avatar answered Sep 20 '22 02:09

maerics


I am using maerics's answer along with error handling. The flag 'a' is used to Open file for appending. The file is created if it does not exist. There Other flags you can use.

// Create a writable stream &  Write the data to stream with encoding to be utf8
    var writerStream = fs.createWriteStream('MockData/output.txt',{flags: 'a'})
                         .on('finish', function() {
                              console.log("Write Finish.");
                          })
                         .on('error', function(err){
                             console.log(err.stack);
                          });


    writerStream.write(outPutData,function() {
      // Now the data has been written.
        console.log("Write completed.");
    });

    // Mark the end of file
    writerStream.end();
like image 39
Hitesh Sahu Avatar answered Sep 21 '22 02:09

Hitesh Sahu