Trying to understand readable streams in node.js

In my module.js I have

var Stream = require('stream');

module.exports = function () {
  var stream = new Stream();

  stream.readable = true;

  stream.emit('data', 'some stuff')
  stream.emit('end')

  return stream;
}

and in my index.js

var module = require('./module')

module().pipe(process.stdout)

substack's example from the stream handbook is working just fine. Why doesn't my code show anything in the command line?

Because you are emitting data before calling pipe, and 'data' listener is attached after first 'data' event is fired

EventEmitter's calls are synchronous (as almost everything else non-IO in node.js)

A bit simplified version of

stream.emit('data', 'some stuff')
stream.pipe(process.stdout)

without EventEmitter could be rewritten as

stream.listeners = [];
// 'emit' call
var ondata = stream.listeners.data;
if (ondata) {
   // only one listener case in the example
   ondata('some stuff');
}
// 'pipe' call
stream.listeners.data = function(buff) {
    process.write(buff);
}