Open In App

Node.js Streams

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Streams are one of the fundamental concepts of Node.js. Streams are a type of data-handling methods and are used to read or write input into output sequentially. Streams are used to handle reading/writing files or exchanging information in an efficient way.

The official Node.js documentation defines streams as “A stream is an abstract interface for working with streaming data in Node.js.” The stream module provides an API for implementing the stream interface. Examples of the stream object in Node.js can be a request to an HTTP server and process.stdout are both stream instances. In short, Streams are objects in Node.js that lets the user read data from a source or write data to a destination in a continuous manner.

Accessing Streams:

const stream = require('stream');

Note: What makes streams powerful while dealing with large amounts of data is that instead of reading a file into memory all at once, streams actually read chunks of data, processing its content data without keeping it all in memory.

Advantages of Streams over other data handling methods:

  • Time Efficient: We don’t have to wait until entire file has been transmitted. We can start processing data as soon as we have it.
  • Memory Efficient: We don’t have to load huge amount of data in memory before we start processing.

Types of Streams in Node.js: There are namely four types of streams in Node.js.

  1. Writable: We can write data to these streams. Example: fs.createWriteStream().
  2. Readable: We can read data from these streams. Example: fs.createReadStream().
  3. Duplex: Streams that are both, Writable as well as Readable. Example: net.socket.
  4. Transform: Streams that can modify or transform the data as it is written and read. Example: zlib.createDeflate.

Some Node APIs that uses streams are:

  • net.Socket()
  • process.stdin()
  • process.stdout()
  • process.stderr()
  • fs.createReadStream()
  • fs.createWriteStream()
  • net.connect()
  • http.request()
  • zlib.createGzip()
  • zlib.createGunzip()
  • zlib.createDeflate()
  • zlib.createInflate()

Implementing a Readable Stream: We will read the data from inStream and echoing it to the standard output using process.stdout.




// Sample JavaScript Code for creating
// a Readable Stream
// Accessing streams
const { Readable } = require('stream');
  
// Reading the data 
const inStream = new Readable({
    read() { }
});
  
// Pushing the data to the stream
inStream.push('GeeksForGeeks : ');
inStream.push(
    'A Computer Science portal for Geeks');
  
// Indicates that no more data is
// left in the stream
inStream.push(null);
  
// Echoing data to the standard output
inStream.pipe(process.stdout);


Output:

GeeksForGeeks : A Computer Science portal for Geeks 

Implementing a Writable Stream: In the outStream, we simply console.log the chunk as a string. We also call the callback function to indicate success without any errors. We will read the data from inStream and echo it to the standard output using process.stdout.




// Sample JavaScript Code for
// Writable Stream
// Accessing Streams
const { Writable } = require('stream');
  
// Whatever is passed in standard 
// input is out streamed here.
const outStream = new Writable({
  
    // The Write function takes three 
    // arguments
    // Chunk is for Buffer
    // Encoding is used in case we want
    // to configure the stream differently
    // In this sample code, Encoding is ignored 
    // callback is used to indicate 
    // successful execution
    write(chunk, encoding, callback) {
        console.log(chunk.toString());
        callback();
    }
  
});
  
// Echo the data to the standard output
process.stdin.pipe(outStream);


Output:

Hello Geeks


Previous Article
Next Article

Similar Reads

Servers, streams and sockets in Node
Node.js Server Node.js is a javascript framework for writing Server-side applications. A Node.js server provides the mechanisms for connecting to a service and sending/receiving data. It achieves this through TCP or UDP connections. Developers can hence create their own server and test their app deployment. NodeJS comes with a simple HTTP server bu
5 min read
Explain the concept of streams in Node
Streams in Node provide an efficient way to handle data flowing through an application. They allow you to read from or write to a source continuously, processing chunks of data as they become available, without needing to load the entire dataset into memory at once. What are Streams?Streams in NodeJS represent a sequence of data that can be read fr
2 min read
Node.js Connect Mysql with Node app
In this article, we will learn how to connect the Mysql database to the NodeJs application. Before we go deep into the coding part, let’s get a brief introduction about these technologies: NodeJs: An open-source platform for executing javascript code on the server side. Also a javascript runtime built on Chrome's V8 JavaScript engine. It can be dow
3 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
There are many different ways to install node.js on a computer. The simplest method to verify whether node.js has been properly installed in your computer is simply type node-v in the command prompt or Windows PowerShell. But many times, it happens, most commonly if you're a beginner, the command prompt prints the output something like this: 'node'
2 min read
Top 3 Best Packages Of Node.js that you should try being a Node.js Developer
Node.js is an open-source and server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. The 3 Best Packages of Node.js that you should
4 min read
Javascript Program For Inserting A Node After The N-th Node From The End
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 <= n. Examples: Input : list: 1->3->4->5 n = 4, x = 2 Output : 1->2->3->4->5 4th node from the end is 1 and insertion has been done after this node. Input : list: 10->8
5 min read
Javascript Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
5 min read
How to do pagination Node.js with MySQL ?
Node.js is a runtime environment like Chrome's V8 JavaScript engine. Node.js is an open-source, cross-platform, and backend runtime environment that executes outside a web browser. MySQL is an open-source relational database management system that is fast, reliable, flexible, and robust. Both MySQL and Node.js are trendy choices when building web a
9 min read
How to resolve unhandled exceptions in Node.js ?
There are two approaches to resolve unhandled exceptions in Node.js that are discussed below: Approach 1: Using try-catch block: We know that Node.js is a platform built on JavaScript runtime for easily building fast and scalable network applications. Being part of JavaScript, we know that the most prominent way to handle the exception is we can ha
2 min read
Article Tags :