Discover how writable streams save your app from crashes and slowdowns when writing data!
Why Writing data with Writable streams in Node.js? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you need to save a large file piece by piece in Node.js by manually opening a file, writing chunks of data, and closing it each time.
Manually handling file writes is slow, error-prone, and can easily cause memory overload or data loss if you forget to manage the flow properly.
Writable streams let you write data efficiently in chunks, handling backpressure and errors automatically, so your app stays fast and reliable.
const fs = require('fs'); const fd = fs.openSync('file.txt', 'w'); fs.writeSync(fd, chunk); fs.closeSync(fd);
const fs = require('fs'); const stream = fs.createWriteStream('file.txt'); stream.write(chunk); stream.end();
Writable streams enable smooth, memory-friendly writing of large or continuous data without blocking your app.
Streaming a video upload directly to disk without loading the entire file into memory first.
Manual file writing is complicated and risky.
Writable streams handle data flow and errors for you.
This makes writing large or ongoing data easy and efficient.
Practice
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]
- Confusing Writable with Readable streams
- Thinking Writable streams read data
- Mixing streams with server creation
_write method in a custom Writable stream?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]
- Forgetting the callback parameter
- Not calling callback inside _write
- Wrong number of parameters in _write
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?
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]
- Assuming chunks concatenate automatically
- Expecting no newline between chunks
- Confusing write and end data handling
const { Writable } = require('stream');
class BrokenStream extends Writable {
_write(chunk, encoding) {
console.log(chunk.toString());
}
}
const stream = new BrokenStream();
stream.write('Test');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]
- Omitting callback parameter
- Not calling callback inside _write
- Confusing _write with write method
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]
- Logging inside _write causing multiple logs
- Not calling callback causing stream to hang
- Using readable stream instead of writable
