Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a NodeJS 'passthrough' stream?

Tags:

stream

node.js

Is there a NodeJS 'passthrough' stream?

i.e. an object where whatever I put in to it comes out immediately, unchanged.

It seems pointless, but it would be useful as a 'static centre' for rapidly changing code during development.

like image 598
fadedbee Avatar asked Oct 18 '13 08:10

fadedbee


People also ask

What is stream PassThrough ()?

PassThrough. This Stream is a trivial implementation of a Transform stream that simply passes the input bytes across to the output.

What types of streams exist in node JS?

There are four fundamental stream types in Node. js: Readable, Writable, Duplex, and Transform streams.

Is Nodejs suitable for video streaming?

Nodejs is very good to streaming audio and video, but nodejs is a new technology, so it's don't have a lot of softwares yet.

What is a writable stream in Nodejs?

Writable : streams to which data can be written (for example, fs. createWriteStream() ). Readable : streams from which data can be read (for example, fs. createReadStream() ). Duplex : streams that are both Readable and Writable (for example, net.


2 Answers

Yeah. Actually, by that very name. :)

stream.PassThrough

It's available with Node 0.10 and later as part of the Streams 2 update (mentioned at the end).

It's also one of the few types from Streams that can be directly instantiated:

var pass = new stream.PassThrough(); 

And, it's currently documented briefly under API for Stream Implementors (towards the bottom of the Steams ToC).

like image 127
Jonathan Lonowski Avatar answered Sep 20 '22 18:09

Jonathan Lonowski


it's really handy when you need to send input bytes of a TCP Server to another TCP Server.

In my microntoller application's web part i am using this as follows

   var net = require('net'),        PassThroughStream = require('stream').PassThrough,        stream = new PassThroughStream();     net.createServer({allowHalfOpen: true}, function(socket) {      socket.write("Hello client!");      console.log('Connected:' + socket.remoteAddress + ':' +    socket.remotePort);      socket.pipe(stream, {end: false});      }).listen(8080);     net.createServer(function(socket) {      stream.on('data', function (d) {       d+='';       socket.write(Date() + ':' + ' ' + d.toUpperCase());     });    socket.pipe(stream);    }).listen(8081); 
like image 35
sukanta Avatar answered Sep 22 '22 18:09

sukanta