Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Streams in Node.js

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:03:00

What are Streams?

Streams are one of the key concepts in Node.js. Streams are a form of data-handling technology that reads or writes input into output in sequence. Streams are used to read/write files and exchange information efficiently.


Types of Streams

There are four types of streams in Node.js.

  1. Writable: We can add data to these streams.
  2. Readable: We can access data from these streams.
  3. Duplex: Streams that are both writeable and readable.
  4. Transform: Streams that can change or transform data as it is written and read.

Properties and Methods for Readable Streams

  • isPaused(): Returns true if the reading stream is now stopped and false otherwise.
  • pause(): Stops the reading stream.
  • pipe(): Converts the reading stream to the specified writable stream.
  • read(): Returns a specific portion of the readable stream.
  • resume(): Restarts a paused stream.
  • setEncoding(): Determines the character encoding of the reading stream.
  • unpipe(): This method prevents the pipe() method from converting a readable stream to a writable one.
  • unshift(): Restores some specified data to the internal buffer.
  • wrap(): Helps in reading streams created by previous Node.js versions.

Properties and Methods for Writable Streams

  • cork(): Stops the writable stream and buffers all written data in memory.
  • end(): Closes the writable stream.
  • setDefaultEncoding(): Configures the encoding for the writable stream.
  • uncork(): Clears all data that has been buffered since the cork() method was invoked.
  • write(): Sends data to the stream.

Creating Streams

To create streams, use the built-in modules fs for file streams and http for network streams. You can also design your streams by extending the Stream basic class.

Piping the Streams

Piping is a method that uses the output of one stream as the input for another. It is typically used to extract data from one stream and pass the output to another stream. There are no restrictions on piping operations.

Chaining the Streams

Chaining is a mechanism for connecting the output of one stream to another, resulting in a chain of several stream actions. It is typically utilized for pipe operations.

Working with Streams

  • Use the .pipe() technique to join readable and writable streams.
  • Handle stream activities by listening for events such as 'data', 'end', and 'error'.
  • Implement readable and writable interfaces for bespoke streams.

Uses of Stream

  • Reading huge files or data via HTTP requests in chunks to avoid loading everything into memory all at once.
  • Writing data to files or network locations in an efficient manner.
  • On-the-fly data transformation options include compression and encryption.

Benefits of Streams

  • Efficient memory use: Streams handle data in parts, lowering memory overhead.
  • Improved performance: Data is processed as soon as it becomes available, rather than waiting for the complete dataset.

Stream-Based Node.js APIs

Here are some Node APIs that use streams:

  • net.Socket(): Provides a duplex stream interface for networking.
  • process.stdin(): Represents a readable stream for standard input.
  • process.stdout(): Represents a writable stream for standard output.
  • process.stderr(): Represents a writable stream for standard error.
  • fs.createReadStream(): Creates a readable stream to read data from a file.
  • fs.createWriteStream(): Creates a writable stream to write data to a file.
  • net.connect(): Creates a duplex stream for TCP client connections.
  • http.request(): Creates a duplex stream for making HTTP requests.
  • zlib.createGzip(): Creates a transform stream to compress data using gzip.
  • zlib.createGunzip(): Creates a transform stream to decompress gzip data.
  • zlib.createDeflate(): Creates a transform stream to compress data using deflate.
  • zlib.createInflate(): Creates a transform stream to decompress and deflate data.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this