# Day 12 of 100days of code.

*As I navigate through the complexities of backend development, I am committed to mastering RESTful API design, authentication mechanisms, data validation, and error handling to ensure the reliability and security of my applications. Lets Goooo💪🏼🚀*

### Fundamentals of nodeJS

## Understanding streams in nodeJS.

In streams we can process data piece by piece instead of reading or writing the whole data at once. Streaming makes the data processing more efficient in terms of memory because there is no need to keep all the data in the memory. In terms of performance and time, streaming starts processing the data as soon as the first chunk of data arrives.

### How to implement streams in nodeJS

There are four types of streams :

* Readable stream
    
* Writable stream
    
* Duplex stream
    
* Transform stream
    

The readable and writable streams are the most important and common ones.

The <mark>readable streams</mark> are the ones from where we can read or consume data chunk by chunk. The request data which we receive from the server, we receive through readable stream. When we send a request to the server a readable stream is opened and through that readable stream we get the request data in chunks. That means the request data which comes on the server comes in piece by piece and not the complete data at once. We can also read a file piece by piece using create read stream from the fs module which can be quite useful for reading large text files. Streams are actually instances of event emitter class. That means all types of streams can emit and listen to named events.

The two most important events of the readable stream are the `data` and the `end` events. The data event is emitted when there is a new piece or new chunk of data to consume. Whenever we are reading a data using the read stream, the `data` will be read in chunks, so whenever a new chunk of data is read and available to use, the data event will be emitted. The `end` event is emitted as soon as there is no more data to consume. And when this event happens, we can react to this events accordingly.

Beside events, we also have important functions that we can use on a stream and in case of readable stream, the most important functions are the `pipe` and `read` functions.

<mark>Writable streams</mark> are the one's which we can write data. It is basically the opposite of readable stream. the HTTP response is a great example of the writable stream because we can send data back to the client. For instance if we want to send a huge video file to a client, we would stream the result just like Netflix or YouTube, instead of sending the complete video at once.

The two most important events of the writable streams are the `drain` and the `finish` events

The `drain` event is raised when the writable streams internal buffer have been emptied and the `finish` event in a writable stream is emitted after the calling of the `end` method, all the data is been flushed to the hidden system.

The most important functions which we have on the writable streams is the `write` and `end` function.

The <mark>Duplex stream</mark> is simply a stream that is both readable and writable at the same time. These types of streams are less common and a good example is the web socket from the net module. A web socket is basically a communication channel between a client and the server which works in both the direction and stays open once the connection has been established. Web sockets are used in real-time chat apps

The <mark>transform stream</mark> are the duplex streams which are both readable and writable and at the same time it can modify and transform the data as it is read or written.

We can also implement our own streams and consumed them.

**Next**: *Understanding streams in practice.*

**See you soon**😊🫡✌🏼...
