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'
Vincent Saluzzo
  • 1,377
  • 1
  • 11
  • 13

2 Answers2

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.

Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
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);};
user568109
  • 47,225
  • 17
  • 99
  • 123
  • 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 ? – Vincent Saluzzo Aug 27 '13 at 09:13
  • 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. – user568109 Aug 27 '13 at 09:36
  • 1
    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 # has no method 'write' – Vincent Saluzzo Aug 28 '13 at 09:29