thinking in sequences - streams in node.js & io.js
TRANSCRIPT
request.on('data', function(data){
response.write(transform(data))
});
Easiest thing that can possibly work… can it?
Readable Stream
Writable Stream
request.on('end', function(){
response.end()
});
Readable.resume() & Readable.pause()
request.on('data', function(data) {
if (response.write(data) == false){
}
});
response.on('drain', function(data) {
request.resume();
});
request.pause();
Example 1
var RStream =
require('stream').Readable;
var rs = new RStream;
rs.push('First Element');
rs.push('Second Element');
rs.push('Third Element');
rs.push(null);
rs.pipe(process.stdout);
var Readable = require('stream').Readable
Subclass stream.Readable
util.inherits(RStream, Readable);
var RStream = function (options){
Readable.call(this, options)
}
Overwritting _read(size) function
RStream.prototype._read = function() {
if (curIndex === data.length)
return this.push(null);
var item = data[curIndex++];
this.push(item);
}
var data = ["First”, "Second”, "Third”];
var curIndex = 0;
Example 2 – Flowing Mode
var readFunct = function(el){
log('received:' + el);
}
str.on('data', readFunct);
str.on('end', function() {
log('All consumed');
});
Example 3 – Non-Flowing Mode
var readFunct = function(){
while (null !== (el = str.read()))
{
log('received:' + str.read()
}
}
str.on('readable', readFunct);
str.on('end', function() {
log('All consumed');
});
var Writable = require('stream').Writable
Subclass stream.Writable
util.inherits(WStream, Writable);
var WStream = function (options){
Writable.call(this, options)
}
Example 1
var WSImpl = function(chunk, encoding, callback) {
};
WriteStream.prototype._write = WSImpl
console.log('write: ' + JSON.stringify(chunk));
callback();
Example 2
var WSImpl = function(chunk, encoding, callback) {
http.request(“foo.bar?p=" + chunk, function(resp){
resp.on(‘end’, callback);
})
};
var Transform =
require('stream').Transformable
Subclass stream.Transformable
Var TStream = function (options){
Transform.call(this, options)
}
…
How
var TSImpl = function(chunk, encoding, callback) {
console.log('transform : ' + chunk.toUpperCase());
this.push(chunk.toUpperCase());
callback();
};
TransformStream.prototype._transform = TSImpl
https://github.com/sindresorhus/awesome-nodejs#streams
Awesome NodeJS - Streams
through2
concat-stream
readable-stream
Pull Push – Streams2 -Pull Push – Streams2 – readable event first
stream.on('readable', function() {
while(var chunk = stream.read()) {
pullD += chunk;
}
});
stream.on('data', function(chunk) {
pushD += chunk;
});
Links
http://nodestreams.com/
Node.js Streams Playground
http://nodeschool.io/
Node.js School – Stream Adventure
https://github.com/substack/stream-handbook
Streams Handbook
Artur Skowroński
Thank You!
… and waiting for questions!@ArturSkowronski
arturskowronski.com
github.com/ArturSkowronski
slideshare.net/ArturSkowroski