If you add a `readable` handler, then `'readable'` (and potentially
`'end'`, if there's no data) will be emitted.
> So the readable event is more of an advisory event. The docs should probably say something about how you could possibly miss the event entirely if you're doing some other IO before you try and read from the stream.
The docs don't say that because it's not true. (Unless, of course,
someone *else* is listening for it, or reading from it, but then it's
not really *your* stream, now, is it?)
This is perfectly safe:
```
net.createServer(function(sock) {
doSomeIOandTHEN(function() {
sock.on('readable', function() {
readStuff(sock)
})
})
})
```
> It seems like we get to pick one API or the other. I can make all of my code use the new API or I can abuse the reverse compatibility, either way I'm only using one API.
I don't see how you can call this "abuse", really. It's not as if
there's "new API" and "old API" living side by side here. There's
just "The API". It's not bad to use any part of it.
One thing that sort of is unfortunate is that there's no way to switch
out of flowing-mode (pause/resume) without wrapping. So far, this
isn't much of a problem, because you usually decide what you'er going
to do with a stream, and then do it. (Either pipe, consume in chunks,
or pause/resume/on('data') it.)
So, for a module like request, you probably want to be a bit less
opinionated. If you know you're going to just collect upthe entire
response body, and pass to a callback, then fine. But if you're going
to pass the stream along, then setting it into flowing-mode is not so
great, because you don't know the user wants that.
> Part of the reason I was so excited about streams2 is that I thought it meant I could finally deprecate BufferedStream in favor of 0.10's Readable.
Part of the reason we wrote streams2 was so that you could deprecate
BufferedStream :)
> streams would spew data regardless of whether or not you had registered for "data" events or called pause().
Both bugs are fixed now.
> But honestly, when you start cracking open node core code just to figure out how things work you start relying on implementation details that may not even be specified fully.
Well, when RTFM doesn't give you the answers you need, then RTFS is
the next step. It could mean that the docs are lacking, and as you
know, help is always welcome there. If you find a question not
answered by the documentation, send a doc patch with the answer when
you do find it.
> Looking back, the data/pause/resume/end/error API isn't too bad.
And it's still there. It's not wrong to use it. Go nuts, really.
> The main problem with it was that a lot of streams didn't really care if you paused them.
There were other problems as well. Node-core has a ton of streams,
and each of them had completely different implementations of all the
streams semantics, with hardly any code reuse at all, and in many
cases, completely unique and opposite bugs and behavior. It was a
nightmare to maintain.
Streams are a core API. Like all core APIs, they're a node paradigm
that it pays to follow and use. But ultimately, like callbacks and
EventEmitters and all the rest, they're there to facilitate the APIs
that node presents. If you'd rather build your own modules in
different ways, go crazy. Really. That's what innovation means.
> Related: the most intuitive streams implementation I've ever used is (gasp!) PHP's. You just call fopen with a URL (file://, http://, zlib://, etc.) and boom, you've got a handle to a stream of data that every other function in the stdlib knows how to use.
Well, of course, we're not going to rely on magic strings, protocol
registration, or have a method called `fopen` that you pass http://
urls. That stuff is just completely crazy.
But if you present a stream for anything (say, a file, http, zlib,
etc.) then you can .pipe() to/from it from/to any other stream. If
you just use .pipe() (or some data collection method like was
presented earlier in this thread) then you have a similar sort of
portability. You can't seek() in streams, but you can read() just as
many bytes as you like. You can even create your own streams or apply
as many filters as you like, and have something that everything else
in the stdlib knows how to deal with.