Writable streams let you send data piece by piece to a destination like a file or network. This helps handle large data smoothly without waiting for everything at once.
Writing data with Writable streams in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
const { Writable } = require('node:stream');
const writable = new Writable({
_write(chunk, encoding, callback) {
// process the chunk
callback();
}
});
writable.write(data);The write method sends data to the stream.
The _write function inside the constructor handles each chunk of data.
Examples
Node.js
const { Writable } = require('node:stream');
const writable = new Writable({
_write(chunk, encoding, callback) {
console.log(chunk.toString());
callback();
}
});
writable.write('Hello');
writable.write(' World!');output.txt.Node.js
const fs = require('node:fs'); const writable = fs.createWriteStream('output.txt'); writable.write('Saving this text to a file.'); writable.end();
Sample Program
This program creates a custom writable stream called Logger that prints each chunk with a prefix. It writes two messages and then ends the stream.
Node.js
const { Writable } = require('node:stream');
class Logger extends Writable {
constructor() {
super();
}
_write(chunk, encoding, callback) {
console.log(`Logging: ${chunk.toString()}`);
callback();
}
}
const logger = new Logger();
logger.write('First message');
logger.write('Second message');
logger.end();Important Notes
Always call callback() inside the _write method to signal completion.
Use stream.end() to close the writable stream properly.
Writable streams help manage memory by processing data in chunks.
Summary
Writable streams let you send data piece by piece to a destination.
Implement the _write method to handle each chunk.
Remember to call callback() and end the stream with end().
Practice
1. What is the main purpose of a Writable stream in Node.js?
easy
Solution
Step 1: Understand Writable stream role
Writable streams are designed to send data to a destination in chunks.Step 2: Compare with other options
Reading data is done by Readable streams, not Writable. Creating servers and handling HTTP requests are unrelated to Writable streams.Final Answer:
To send data piece by piece to a destination -> Option AQuick Check:
Writable stream = send data [OK]
Hint: Writable streams send data out chunk by chunk [OK]
Common Mistakes:
- Confusing Writable with Readable streams
- Thinking Writable streams read data
- Mixing streams with server creation
2. Which of the following is the correct way to implement the
_write method in a custom Writable stream?easy
Solution
Step 1: Recall _write method signature
The _write method must accept three parameters: chunk, encoding, and callback.Step 2: Check callback usage
Calling callback() signals that the chunk was processed. Omitting it causes the stream to hang.Final Answer:
_write(chunk, encoding, callback) { callback(); } -> Option AQuick Check:
_write needs callback() [OK]
Hint: Always include callback in _write and call it [OK]
Common Mistakes:
- Forgetting the callback parameter
- Not calling callback inside _write
- Wrong number of parameters in _write
3. Consider this code snippet:
What will be printed to the console?
const { Writable } = require('stream');
class MyStream extends Writable {
_write(chunk, encoding, callback) {
console.log(chunk.toString());
callback();
}
}
const stream = new MyStream();
stream.write('Hello');
stream.end('World');What will be printed to the console?
medium
Solution
Step 1: Understand write and end calls
stream.write('Hello') sends 'Hello' chunk, then stream.end('World') sends 'World' chunk and ends.Step 2: Check _write behavior
Each chunk is logged separately with console.log, so 'Hello' and 'World' print on separate lines.Final Answer:
Hello World printed separately -> Option DQuick Check:
Each chunk logs separately [OK]
Hint: Each write chunk logs on its own line [OK]
Common Mistakes:
- Assuming chunks concatenate automatically
- Expecting no newline between chunks
- Confusing write and end data handling
4. What is wrong with this Writable stream implementation?
const { Writable } = require('stream');
class BrokenStream extends Writable {
_write(chunk, encoding) {
console.log(chunk.toString());
}
}
const stream = new BrokenStream();
stream.write('Test');medium
Solution
Step 1: Check _write method signature
_write must have three parameters: chunk, encoding, callback.Step 2: Check callback usage
Callback must be called to signal completion; missing callback causes stream to hang.Final Answer:
Missing callback parameter and not calling callback() -> Option BQuick Check:
_write needs callback param and call [OK]
Hint: Always include and call callback in _write [OK]
Common Mistakes:
- Omitting callback parameter
- Not calling callback inside _write
- Confusing _write with write method
5. You want to create a Writable stream that collects all written chunks into a single string and logs it only when the stream ends. Which approach is correct?
hard
Solution
Step 1: Collect chunks inside _write
Inside _write, append each chunk to a variable and call callback to continue.Step 2: Log combined data on 'finish' event
Listen to the 'finish' event to know when writing ends, then log the full collected string.Final Answer:
Store chunks in a variable inside _write, call callback, then log in 'finish' event -> Option CQuick Check:
Collect chunks + log on finish = Store chunks in a variable inside _write, call callback, then log in 'finish' event [OK]
Hint: Collect chunks in _write, log on 'finish' event [OK]
Common Mistakes:
- Logging inside _write causing multiple logs
- Not calling callback causing stream to hang
- Using readable stream instead of writable
