New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is objectMode a replacement? #30

Closed
joliss opened this Issue May 4, 2013 · 4 comments

Comments

Projects
None yet
2 participants
@joliss

joliss commented May 4, 2013

Node's stream API as of nodejs/node-v0.x-archive@444bbd4 has a (somewhat under-documented) objectMode flag. Do you know if this a replacement for event-stream?

I need something like event-stream for my library, and now I'm unsure which one I should use.

@dominictarr

This comment has been minimized.

Owner

dominictarr commented May 4, 2013

so, objectMode disables some stuff that stream2 does for streams of buffers,
where as event-stream just provides some functions for generating certain types of streams.

I pretty much never use event-stream any more. the most useful thing to come out of it is through and maybe split which uses through anyway. streams2.Transform is basically the same idea as through.

streams2 is quite complicated. though, the benefit is that it buffers automatically, so you can do async stuff before you pipe. however, if you use through (and this.queue(data)) then it will buffer also, but you have to explicitly call .pause() to prevent data coming through.

This usually isn't a big deal with the sort of things you want object streams for, anyway.
personally, I still use classic streams, because they are much less complicated for this sort of thing.
you can also turn classic streams back into new style streams if you want with streams2.wrap.

also note, that since streams2 buffers automatically, if you don't actually want to read from a stream,
you have to dump it explicitly.

http.createServer(req, res) {
  req.resume() //drop anything
  res.end('hello')
})

win some, loose some...

@joliss

This comment has been minimized.

joliss commented May 4, 2013

Very cool, thanks for elucidating! :)

@joliss joliss closed this May 4, 2013

@joliss

This comment has been minimized.

joliss commented May 8, 2013

For posterity: It seems I misunderstand the purpose of the event-stream library. It's just a loose collection of stream functions (rather than an object-based stream like I thought for some reason).

As substack pointed out to me, for sequences of objects, a better alternative to streams (or the objectStream feature) is using EventEmitters, sending one object per event.

@dominictarr

This comment has been minimized.

Owner

dominictarr commented May 8, 2013

streams2 has changed things a bit, but when this library was first written, all the node stream stuff worked equally with streams of objects, and streams of text. In streams2 you have to explicitly declare that a stream is an object stream, else it will attempt to concatenate chunks, which of course, must be strings or buffers.

event-stream is indeed a collection of stream functions - mostly intended for streams of objects.

I argue against just using event emitters, because, once you have a few things connected together, and you need to handle some edge cases - like errors, or the end of messages, or disconnections, then you pretty much need a source.pipe(dest) function.

In node 0.4, 0.6, 0.8 the same Stream#pipe worked with objects and text, and pretty much had the same edge cases
(in fact, often you are converting from a stream of text into a stream of objects, and vice versa, via a parser/stringifier)

If you just use an event emitter then you'll only end up reinventing the Stream#pipe function... and probably doing it badly - as an incredible amount of work went into getting it right.

Incidentally, I use this now https://github.com/dominictarr/pull-stream

it's much simpler that streams2, and much more powerful for object streams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment