Bird
Raised Fist0
Node.jsframework~30 mins

Stream types (Readable, Writable, Transform, Duplex) in Node.js - Mini Project: Build & Apply

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Node.js Stream Types: Readable, Writable, Transform, Duplex
📖 Scenario: You are building a simple Node.js program that demonstrates how different stream types work. Streams help handle data piece by piece, like reading a book page by page instead of all at once.This project will show how to create and use four stream types: Readable, Writable, Transform, and Duplex.
🎯 Goal: Build a Node.js script that creates one stream of each type:A Readable stream that emits three lines of text.A Writable stream that collects and stores data it receives.A Transform stream that changes all text to uppercase.A Duplex stream that can both read and write data, here it will reverse the text.Connect these streams to see how data flows and changes.
📋 What You'll Learn
Create a Readable stream emitting exactly these strings: 'Hello', 'Node.js', 'Streams'
Create a Writable stream that stores received data in an array called collectedData
Create a Transform stream that converts input text to uppercase
Create a Duplex stream that reverses the text it receives and outputs it
Pipe the Readable stream through the Transform stream, then into the Writable stream
Demonstrate writing and reading from the Duplex stream
💡 Why This Matters
🌍 Real World
Streams are used in Node.js to efficiently handle large files, network data, or any data that comes in chunks, like video streaming or reading big logs.
💼 Career
Understanding streams is essential for backend developers working with Node.js, especially for building fast, memory-efficient applications that process data continuously.
Progress0 / 4 steps
1
Create a Readable stream emitting fixed text
Write code to create a Readable stream called myReadable using Node.js stream module's Readable class. It should emit exactly these strings in order: 'Hello', 'Node.js', and 'Streams'. Use the read() method to push these strings and then push null to signal the end.
Node.js
Hint

Use new Readable({ read() { ... } }) and inside read() push the strings one by one, then push null.

2
Create a Writable stream to collect data
Add code to create a Writable stream called myWritable using Node.js Writable class. It should collect all data chunks it receives into an array named collectedData. Initialize collectedData as an empty array before creating the stream. Implement the write(chunk, encoding, callback) method to push each chunk converted to string into collectedData and call callback().
Node.js
Hint

Initialize collectedData as an empty array. Use new Writable({ write(chunk, encoding, callback) { ... } }) and push each chunk as string into collectedData.

3
Create a Transform stream to uppercase text
Add code to create a Transform stream called myTransform using Node.js Transform class. Implement the _transform(chunk, encoding, callback) method to convert the chunk to uppercase string and push it forward. Call callback() after pushing.
Node.js
Hint

Use new Transform({ _transform(chunk, encoding, callback) { ... } }). Convert chunk to uppercase string and push it.

4
Create a Duplex stream to reverse text and connect streams
Add code to create a Duplex stream called myDuplex using Node.js Duplex class. Implement _write(chunk, encoding, callback) to store the chunk string reversed in a variable lastData and call callback(). Implement _read() to push lastData and then push null. Then pipe myReadable through myTransform into myWritable. Finally, write the string 'Node' to myDuplex and read from it by attaching a data event listener that pushes received data into an array duplexOutput.
Node.js
Hint

Use new Duplex({ _write(chunk, encoding, callback) { ... }, _read() { ... } }). Reverse the string in _write and push it in _read. Pipe the streams as instructed. Write and read from myDuplex and collect data in duplexOutput.

Practice

(1/5)
1. Which type of Node.js stream is designed to only provide data piece by piece for reading?
easy
A. Transform stream
B. Readable stream
C. Writable stream
D. Duplex stream

Solution

  1. Step 1: Understand stream roles

    Readable streams are designed to emit data chunks for consumption.
  2. Step 2: Match stream type to description

    Only readable streams provide data piece by piece without accepting input.
  3. Final Answer:

    Readable stream -> Option B
  4. Quick Check:

    Readable = provides data [OK]
Hint: Readable streams only output data, no input accepted [OK]
Common Mistakes:
  • Confusing writable streams as data providers
  • Thinking transform streams only read data
  • Mixing duplex with readable-only behavior
2. Which of the following is the correct way to create a writable stream in Node.js?
easy
A. const stream = new Readable();
B. const stream = new Duplex();
C. const stream = new Transform();
D. const stream = new Writable({ write(chunk, encoding, callback) { callback(); } });

Solution

  1. Step 1: Identify writable stream creation

    Writable streams require a write method to handle incoming data chunks.
  2. Step 2: Check options for correct syntax

    const stream = new Writable({ write(chunk, encoding, callback) { callback(); } }); correctly creates a Writable stream with a write method and callback.
  3. Final Answer:

    const stream = new Writable({ write(chunk, encoding, callback) { callback(); } }); -> Option D
  4. Quick Check:

    Writable needs write() method [OK]
Hint: Writable streams need a write() method in options [OK]
Common Mistakes:
  • Using Readable constructor for writable stream
  • Omitting write method in Writable options
  • Confusing Transform or Duplex constructors
3. What will be the output of the following code snippet?
const { Transform } = require('stream');
const upperCase = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    callback();
  }
});

upperCase.on('data', data => console.log(data.toString()));
upperCase.write('hello');
upperCase.end();
medium
A. HELLO
B. hello
C. undefined
D. Error: callback not called

Solution

  1. Step 1: Understand Transform stream behavior

    The transform method converts input chunk to uppercase and pushes it forward.
  2. Step 2: Analyze event and output

    The 'data' event logs the transformed chunk, which is uppercase 'HELLO'.
  3. Final Answer:

    HELLO -> Option A
  4. Quick Check:

    Transform changes data to uppercase [OK]
Hint: Transform streams modify data before output [OK]
Common Mistakes:
  • Expecting original lowercase output
  • Forgetting to call callback() in transform
  • Assuming no output without explicit read
4. Identify the error in this Duplex stream implementation:
const { Duplex } = require('stream');
const duplex = new Duplex({
  read(size) {
    this.push('data');
  },
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
});
duplex.on('data', data => console.log('Received:', data.toString()));
duplex.write('hello');
medium
A. The Duplex constructor is missing the object mode option
B. The write method is missing the callback call
C. The read method should push null to signal end of data
D. The duplex stream cannot both read and write

Solution

  1. Step 1: Check read method behavior

    The read method pushes 'data' but never signals end by pushing null.
  2. Step 2: Understand stream end signaling

    Streams must push null to indicate no more data; missing here causes hanging.
  3. Final Answer:

    The read method should push null to signal end of data -> Option C
  4. Quick Check:

    read() must push null to end stream [OK]
Hint: read() must push null to end data stream [OK]
Common Mistakes:
  • Not pushing null in read method
  • Forgetting callback in write method
  • Assuming Duplex can't read and write
5. You want to create a custom Duplex stream that reads numbers from 1 to 3 and writes their squares. Which approach correctly implements this behavior?
hard
A. Push numbers 1 to 3 in read(), write() logs squares, and push null after 3
B. Push squares in read(), write() pushes numbers 1 to 3, no null needed
C. Use Transform stream instead of Duplex for this task
D. Only Writable stream can handle both reading and writing

Solution

  1. Step 1: Understand Duplex stream roles

    Duplex streams can read and write independently; read() pushes data, write() processes input.
  2. Step 2: Match behavior to implementation

    Push numbers 1 to 3 in read(), write() logs squares, and push null after last number to end stream.
  3. Final Answer:

    Push numbers 1 to 3 in read(), write() logs squares, and push null after 3 -> Option A
  4. Quick Check:

    Duplex reads numbers, writes squares [OK]
Hint: Duplex streams read and write independently [OK]
Common Mistakes:
  • Confusing Transform with Duplex for this task
  • Not pushing null to end read stream
  • Thinking Writable streams can read data