What is Streams3 in Node.js and how is it different from Streams2?

I often heard about Streams2 and old threads, but what is Streams3? This is mentioned in this talk of Torsten Lorenz .

Where can I read about this, and what is the difference between Streams2 and Streams3.

When I search on Google, I also see that it is mentioned in the Node 0.11.5 changelog ,

stream: stream simplification, passive data listening (streams3) (isaacs)

+46
stream
Feb 03 '14 at 22:04
source share
3 answers

I am going to do this, but I probably made a mistake. Never recording Streams1 (old streams) or Streams2, I'm probably not the kind of guy who could answer this question on his own, but here everything goes. There seems to be an API Streams1 which is preserved to some extent. Streams2 has two stream modes (inherited) and non-streaming. In short, a gasket that supports the current mode is leaving. This was a message that led to the patch now called Streams3 ,

Same API as streams2, but removes the confusing modality of the stream / old mode switch.

  • Each time read() is called and returns some data, a data event occurs.
  • resume() will make it re-call read (). Otherwise no change.
  • pause() will cause read() called again.
  • pipe(dest) and on('data', fn) will automatically call resume() .
  • No switches in old mode. There only flowing and stopped. Streams begin with a pause.

Unfortunately, to understand any description that describes Streams3 very well, you need to understand Streams1 first, and legacy streams

Background

First, let's see what Node v0.10.25 says about the two modes,

Read streams have two “modes”: stream mode and non-stream mode. When in streaming mode, data is read from the base system and delivered to your program as quickly as possible. In non-flowing mode, you must explicitly call stream.read () to get pieces of data. - Node v0.10.25 Documents

Isaac Z. Schlüter said in November the slides I dug up :

streams2

  • "suck streams"
  • Instead of emitting data events, call the read () function to retrieve data from the source.
  • Solves all problems (of which we know)

So it seems that in streams1 you should create an object and call .on('data', cb) for that object. This would set the event as a trigger, and then you were in the grip of the flow. In Streams2 streams, internal streams have buffers and you request data from these streams explicitly (using `.read). Isaac continues to point out how backward compatibility works in Streams2 to support Streams1 (old-stream) modules

stream of old mode streams1

  • New threads can switch to the old mode, where they display "data"
  • If you add a data event handler or call pause () or resume (), then toggle
  • Making minimal changes to existing tests to keep us informed.

So, in Streams2, calling .pause() or .resume() starts laying out. And it must be right? In Streams2, you have control when to .read() , and you won’t catch the things that throw you. This caused an outdated mode that acted independently of Streams2.

Take an example from the Isaac slide,

 createServer(function(q,s) { // ADVISORY only! q.pause() session(q, function(ses) { q.on('data', handler) q.resume() }) }) 
  • In threads 1, q starts reading and emitting immediately (probably data loss) until q.pause q to stop data extraction, but not due to emitting events to clear what it has already read.
  • In Streams2, q begins to pause until .pause() called, which means emulating the old mode.
  • In Streams3, q starts as paused, never reads from the file descriptor, making q.pause() noop, and when q.on('data', cb) is q.resume , it calls q.resume until there is more data in the buffer, AND then call q.resume again, doing the same.
+41
Feb 04 '14 at 10:02
source share
— -

It seems that Streams3 was introduced in io.js, then in Node 0.11 +

Streams 1 Supported data is transferred to the stream. There was no consumer control; data was thrown at the consumer whether it was ready or not.

Streams 2 allow you to transfer data to a stream in accordance with streams 1 or so that the consumer retrieves data from the stream as necessary. The user can control the data flow in pull mode (using stream.read () when notified of available data). At the same time, the stream cannot support simultaneous pressing and shutting down.

Threads 3 allow you to retrieve and push data in a single thread.

Great review here:

https://strongloop.com/strongblog/whats-new-io-js-beta-streams3/

+6
Jul 10 '15 at 16:45
source share

I suggest you read the documentation, namely the "API for streaming consumers" section, this is really very clear, in addition, I think the other answer is incorrect: http://nodejs.org/api/stream.html#stream_readable_read_size

-3
Feb 20 '15 at 20:34
source share



All Articles