Stream in Node. js introduction _ node. js

Source: Internet
Author: User
This article mainly introduces Node. stream in js. This article describes what Stream, pipe method, Stream classification, and Readable Stream status switching. For more information, see What is stream?

When it comes to stream, it involves the concept of * nix: pipeline-In * nix, the stream is implemented in Shell as data that can be bridge through | (pipeline operator, the output (stdout) of a process can be directly used as the input (stdin) of the next process ).

In Node, the concept of Stream is similar to that of a data Stream.

Pipe

The essence of streaming is the. pipe () method. The ability to bridge is that the two ends of the data stream (upstream/downstream or read/write stream) are bridging using a. pipe () method.

The pseudocode format is:

The Code is as follows:


// Upstream. pipe (downstream)
Readable. pipe (Writable );

Stream classification

We do not plan to discuss the so-called "classic" stream before Node v0.4. The stream is divided into several types (all of which are abstract interfaces:

1. stream. Readable stream (the _ read method needs to be implemented, focusing on the reading details of the data stream
2. stream. Writable stream (the _ write method needs to be implemented, focusing on the details of writing data streams
3. stream. Duplex read/write stream (the above two interfaces need to be implemented, focusing on the details of the above two interfaces
4. stream. Transform inherits from Duplex (the _ transform method must be implemented. The focus is on data block processing.

To put it simply:

1 ). the pipe () owner must have the Readable stream (not limited to) capability, it has a series of events including 'readable'/'data'/'end'/'close'/'error', which can be subscribed to or provided. read ()/. pause ()/. resume () and other methods for calling;
2 ). the pipe () parameter must be capable of Writable stream (not limited, it has the 'drain'/'pipe'/'unpipe'/'error'/'finish 'event available for access and is also available. write ()/. end () and other methods for calling

What ghosts

Is there any anxiety? Don't worry. As a low-level code engineer, I will open Stream generator and talk to you.

Stream class, defined in the Node. js Source Code as follows:

The Code is as follows:


Var EE = require ('events'). EventEmitter;
Var util = require ('til ');
Util. inherits (Stream, EE );

Function Stream (){
EE. call (this );
}

It can be seen that Stream is essentially an EventEmitter, which means it has the event-driven function (. emit/. on ...). As we all know, "Node. js is an event-driven platform based on V8". It implements event-driven stream programming and features the same asynchronous callback as Node.

For example, in a Readable stream, there is a readable event. In a paused read-only stream, if a data block is ready for Readable, it will be sent to the subscriber (What are the Readable streams? In express, the req. part of the Upload Component is ftp or mutli-form, and the standard input process. stdin in the system ). With the readable event, we can use a tool like analyzer to process shell command output:

The Code is as follows:


Process. stdin. on ('readable', function (){
Var buf = process. stdin. read ();
If (buf ){
Var data = buf. toString ();
// Parsing data...
}
});

Call this method as follows:

The Code is as follows:


Head-10 some.txt | node parser. js

For a Readable stream, we can also subscribe to its data and end events to obtain data blocks and receive notifications when the stream is exhausted, as in the typical socket example:

The Code is as follows:


Req. on ('connect ', function (res, socket, head ){
Socket. on ('data', function (chunk ){
Console. log (chunk. toString ());
});
Socket. on ('end', function (){
Proxy. close ();
});
});

Readable stream status Switching
Note that the Readable stream has two statuses: flowing mode and pause mode ). The former cannot be stopped at all, and whoever is put on pipe will be immediately suspended. The latter will not read data blocks until the downstream explicitly calls Stream. read () requests. The Readable stream is pause mode during initialization.

The two statuses can be switched to each other,

Pause is converted to flowing for any of the following actions:

1. Add a data event subscription to The Readable stream
2. Call. resume () to explicitly enable flowing for Readable
3. Call the. pipe (writable) of the Readable stream and bridge it to a Writable stream.

Flowing is switched back to pause if any of the following actions are performed:

1. The Readable stream has not been pipe to any stream and can be called. pause () paused.
2. The Readable stream has already been pipe to the stream. You need to remove all data event subscriptions and call the. unpipe () method to cancel the relationship with the downstream stream one by one.

Wonderful use

Combined with the asynchronous nature of the stream, I can write an application like this: directly bridge user A's output to user B's page for output:

The Code is as follows:


Router. post ('/Post', function (req, res ){
Var destination = req. headers ['destination']; // to whom
Cache [destionation] = req;
// Yes, it does not return, so it is best to make an ajax request
});

When user B requests:

The Code is as follows:


Router. get ('/inbox', function (req, res ){
Var user = req. headers ['user'];
Cache. find (user, function (err, previusreq) {// locate the previously stored req
Var form = new multiparty. Form ();
Form. parse (previusreq); // you have a file for me.
Form. on ('part', function (part ){
Part. pipe (res); // good streaming mode :)

Part. on ('error', function (err ){
Console. log (err );
Messaging. setRequestDone (uniqueID );
Return res. end (err );
});
});
});
});

Reference

How to write node programs with streams: stream-handbook

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.