Node.js Stream writable._write() Method

11 Oct, 2021
The writable._write() method is an inbuilt application programming interface of Stream module which is used to implement a writable stream. The writable._write() method is affixed with an underscore as it is inside the class that defines it. Moreover, the user program must not call it directly. This method implemented by using child classes and it is called by the internal Writable class methods only.
Syntax:
writable._write( chunk, encoding, callback )
Parameters: This method accept three parameters as mentioned above and described below:
chunk: It is the data to be written which can be of type buffer, string or any.
encoding: It is the type of encoding to be used if chunk is of type string.
callback: It is a method which is called to check if the write completed or failed. The first argument passed to the callback must be “Error” object if the call didn’t succeeded and null if the write succeeded.
Below examples illustrate the use of writable._write() method Node.js:
Example 1:
// Node.js program to demonstrate the     // writable._write() method  // Constructing writable streamconst {Writable} = require("stream");  // Function to check charconst charchecks = new Writable({    // Implementing write function  write(chunk, encoding, callback){      // Defining string    const string = chunk.toString();          // If string contains below character    // then an error is show else the    // written string is returned    if(string.includes("\/")){      callback(Error("Forbidden character"));          }    else    {      // Displays string      console.log(string);      callback();    }  }});  // Piping standard input to standard output, if// you don't enter the forbidden character else// it throws errorprocess.stdin.pipe(charchecks).on('error', console.log);  // Enter the string to be writtenconsole.log("Enter the string: ");
Now, you need to run the code and enter the String in run time to get the output.
Enter the string:
GeeksforGeeks
GeeksforGeeks // Output

Enter the string:
GfG
GfG // Output

Enter the string:
Nidhi
Nidhi //Output

Now, to exit it you need to press control + C.
Example 2:
// Node.js program to demonstrate the     // writable._write() method   // Constructing writable streamconst {Writable} = require("stream");   // Function to check charconst charchecks = new Writable({     // Implementing write function  write(chunk, encoding, callback){       // Defining string and encoding it    const string = chunk.toString('hex');       // Prints encoded string    console.log(string);           // If the encoded string contains below     // character then an error is shown else    // the length of the encoded string is     // returned    if(string.includes("c")){               callback(Error("This is an error."));           }    else    {      // Displays length of the encoded string      console.log(string.length);      callback();    }  }});   // Piping standard input to standard output, if// you don't enter the forbidden character else// it throws an errorprocess.stdin.pipe(charchecks).on('error', console.log);   // Enter the string to be writtenconsole.log("Enter the string: ");
Now, you need to run the code and enter the String in run time to get the output.Output:
Enter the string:
Geeks
4765656b730a   // encoded string
12             // length of encoded string
Nidhi
4e696468690a
12
portal
706f7274616c0a   // encoded string contains "c" so length of it  
                 // is not returned and an error is thrown
Error: This is an error.
    at Writable.write [as _write] (/home/runner/QuickwittedDistantCensorware/index.js:25:16)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at Writable.write (_stream_writable.js:299:11)
    at ReadStream.ondata (_stream_readable.js:710:20)
    at ReadStream.emit (events.js:198:13)
    at ReadStream.EventEmitter.emit (domain.js:448:20)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at ReadStream.Readable.push (_stream_readable.js:224:10)

Now, to exit it you need to press control C.So, here an error is thrown as the run time input includes the stated character which is forbidden character so, an error is thrown.
Reference: https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback_1
Node.js-Stream-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
Mongoose | findByIdAndUpdate() Function
JWT Authentication with Node.js
Node.js forEach() function
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React