0

I need some help to understand how stream work in NodeJS

I explain, i need to write a module which call a UNIX process (with spawn) and I want to redirect the stdout of this process to a Readable Stream.

I want this behavior to exports the Readable Stream and allow another module to read them.

To do this, I have write a little piece of code :

var spawn = require('child_process').spawn
var Duplex = require('stream').Duplex;
var stream = new Duplex;


var start = function() {
    ps = spawn('mycmd', [/*... args ...*/]);
    ps.stdout.pipe(stream);
};


exports.stream = stream;
exports.start = start;

But if I use this module I throw an exception which say that the stream doesn't implement the _read method.

Can you help me with this problem ?

Thanks in advance.

[EDIT] I have try the solution of creating a Stream object, but that's doesnt work, here is the code:

var spawn = require('child_process').spawn;
var Stream = require('stream');
var ps = null;

var audio = new Stream;
audio.readable = audio.writable = true;

var start = function() {
    if(ps == null) {
        ps = spawn('mycmd', []);

        ps.stdout.pipe(stream);
    }
};

var stop = function() {
    if(ps) {
        ps.kill();
        ps = null;
    }
};


exports.stream = stream;
exports.start = start;
exports.stop = stop;

But when I try to listen the stream, I encount an new error :

_stream_readable.js:583
    var written = dest.write(chunk);
                       ^
TypeError: Object #<Stream> has no method 'write'
2
  • Do you want to use stream or you want to implement stream ? Commented Aug 27, 2013 at 8:24
  • I want to purpose a Readable stream for user which use my module, and I want to fill this stream with the data provided by the stdout of my spawned process Commented Aug 27, 2013 at 8:28

2 Answers 2

2

Most of Node's Stream classes aren't meant to be used directly, but as the base of a custom type:

Note that stream.Duplex is an abstract class designed to be extended with an underlying implementation of the _read(size) and _write(chunk, encoding, callback) methods as you would with a Readable or Writable stream class.

One notable exception is stream.PassThrough, which is a simple echo stream implementation.

var PassThrough = require('stream').PassThrough;
var stream = new PassThrough;

Also note that ps will be a global, making it directly accessible in all other modules.

Sign up to request clarification or add additional context in comments.

1 Comment

Great ! That's go the Job !
0

If you simply want to use stream then you should do :

var stream = new Stream;
stream.readable = stream.writable = true;

Duplex is meant for developers. Some methods like _read and _write need to be implemented for it.

[Update]

OK, you have data source, from the stdout. You will need write function, use this :

stream.write = function(data){this.emit('data', data);};

3 Comments

Ok, so i have maid a confusion ? I try to using Stream as a stream provider but in fact, even if I push data to a Stream, the behavior I want isn't for Stream provider ?
As Stream provider you would use ReadableStream for reading from underlying source e.g. file on disk. You would have to implement file opening, reading, pushing etc. What you want is a stream in your library, I think, so that others can read from it. If you want it for piping you don't need to implement a stream.
Ok, so i've try your solution but when I try to read from the stream I throw an exception : _stream_readable.js:583 var written = dest.write(chunk); ^ TypeError: Object #<Stream> has no method 'write'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.