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

Please more examples! #58

Closed
giacecco opened this Issue Apr 24, 2014 · 5 comments

Comments

Projects
None yet
3 participants
@giacecco

giacecco commented Apr 24, 2014

Dear dominctarr,
Thank you for your work with event-stream, I have been using it for a few days now but still feel like I am just scraping the surface. There are things that I am sure are feasible using event-stream, but the documentation is not sufficient for me to understand how to put the pieces together. Please provide more examples with it!

My current issue in particular is that I need to transform a series of 'events' into a readable stream. The code should look something like the one below, where 'Emitter' is the constructor for a third-party object calling the function I give as a parameter every time there is new data to process.

var outStream = require('fs').createWriteStream("foo.txt", { 'encoding': 'utf-8' });
var inStream = require('event-stream').????;
var emitter = new Emitter(function (data) {
    // there's new data to push as part of the readable stream!
    inStream.????(data);
});
inStream.pipe(outStream);

Can you give me a hint? Thanks,

Giacecco

@timoxley

This comment has been minimized.

timoxley commented Apr 24, 2014

@giacecco you can just write to the destination stream directly

e.g.

emitter.on('data', function(data) {
   destination.write(data)
})
@giacecco

This comment has been minimized.

giacecco commented Apr 25, 2014

Ok but which of event-stream's objects is ideal for 'destination', considering that the event generates data in many events? Moreover, I guess I need to use .pause() and .resume() between one event and another if I want destination to understand that the input stream has not finished, or I did not get how that works too?

In general, if not by experimenting with streams, what can one learn about it before fiddling with event-stream? This info would be useful to be at the top of the README.md for people who gets to your library without having enough experience of the model.

G.

@giacecco

This comment has been minimized.

giacecco commented Apr 25, 2014

It looks like I may have solved with something that looks like below:

var outStream = require('fs').createWriteStream('foo.txt', { 'flags': 'w', 'encoding': 'utf-8' }),
    inStream = require('event-stream').through(function write(data) {
        this.emit('data', data);
    },
    function end () { 
        this.emit('end');
    });
inStream.pipe(outStream);
var emitter = new Emitter(function (data) {
    inStream.write(data);
});

Apparently there is no need for using pause, actually doing that causes a Nodejs warning:

.(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.

It is clear I am still missing the point of how event-stream works, this feels too much like magic :-)

G.

@timoxley

This comment has been minimized.

timoxley commented Apr 25, 2014

Where is this data coming from? What is Emitter?

You can just write directly to your writable stream:

var outStream = require('fs').createWriteStream('foo.txt')
var emitter = new Emitter(function (data) {
    outStream.write(data);
});
@timoxley

This comment has been minimized.

timoxley commented Apr 25, 2014

@giacecco if you want to master this stuff, you should start by doing stream-adventure: http://nodeschool.io/#stream-adventure

@right9ctrl right9ctrl closed this Oct 13, 2018

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