Tute 04: Streams in nodejs - ariffira/node-basic GitHub Wiki
Streams:
- collections of data as arrays, strings and others. e.g: videos, audios data, comments, chats messages etc.
- important when you do not know about data size and its coming and updating always.
- use in Big data, a very hot topic
- stream data come as chunk means set of data package and then one chunk add to the next chunk and repeatedly it extends.
- Nodejs has abstract interface for working with streaming data, e.g : stream module
Nodejs Stream Module:
- It has 4 types of streams:
- Readable // can read 'r'
- Writable // can write new file or update 'w'
- Duplex // both 'w/r' operation
- Transform // output from input as duplex stream
- each stream: EventEmitter instance (e.g: events used data, end (nothing to read), error, finish (for last chunk come), close)
Lets have some code ;)
Create a folder called myStream in node-basic/myStream and then create index.js file as server and copy my server.js code there if you are following all my tute from this git repo.
So the structure of this file: node-basic/myStream/index.js
Create a new module node-basic/myStream/readStream.js
and file node-basic/myStream/file.txt
and write some text inside text file.txt. Check below code for module:
Read a Stream module:
const fs = require("fs");
var somedata = '';
function readStream() {
// Create a readable stream
const readerStream = fs.createReadStream('myStream/file.txt');
// Set the encoding to be utf8.
readerStream.setEncoding('UTF8');
// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk) {
somedata += chunk;
});
readerStream.on('end',function(){
console.log(somedata);
});
readerStream.on('error', function(err){
console.log(err);
});
console.log("Successfully read the streaming data from file.txt..............");
}
module.exports = readStream;
and also index.js as:
....
// include readStream Module to read stream
var readStream = require('./readStream');
// create a server with http
const server = http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('Read Stream works fine!!!');
readStream();
});
...
Open your terminal and check console and also load localhost:5000 and you will see streaming file in console.
Read these for details: