danmactough / node-feedparser

Robust RSS, Atom, and RDF feed parsing in Node.js
Other
1.97k stars 192 forks source link

'end' event never getting emitted #174

Closed douglas-b-clark closed 8 years ago

douglas-b-clark commented 8 years ago

When parsing a feed using feedparser, I never get an 'end' event. I do get a 'meta' event, a 'readable' event, and a 'drain' event after the readable event. I expected the 'end' event to be called after the 'meta' and 'readable' events.

code I'm using:

var fs = require('fs'); var stream = require('stream'); var FeedParser = require('feedparser');

var readableStream = fs.createReadStream('feed.xml');

var feedparser = new FeedParser(); feedparser.on('error', function(err) { console.log('error event: ' + err) });

feedparser.on('end', function () { console.log('end event'); });

feedparser.on('meta', function () { console.log('meta event'); });

feedparser.on('readable', function() { console.log('readable event'); });

feedparser.on('drain', function() { console.log('drain event'); })

readableStream.pipe(feedparser);

Output I get when parsing any valid feed:

meta event readable event drain event

danmactough commented 8 years ago

@douglas-b-clark Ah, what's happening here is you're not reading the feedparser stream, so it's not actually doing anything. The input stream is done and all the input is buffered waiting for you to read it. If we change your script, you'll get an end event:

var fs = require('fs');
var path = require('path');
var stream = require('stream');
var FeedParser = require('..');

var readableStream = fs.createReadStream(path.resolve(__dirname, '..', 'test/feeds/rss2sample.xml'));
readableStream.on('error', console.error);
readableStream.on('end', function () {
    console.log('readableStream end event');
});
readableStream.on('drain', function() {
    console.log('readableStream drain event');
})

var feedparser = new FeedParser();
feedparser.on('error', function(err) {
    console.log('feedparser error event: ' + err)
});

feedparser.on('end', function () {
    console.log('feedparser end event');
});

feedparser.on('finish', function () {
    console.log('feedparser finish event');
});

feedparser.on('meta', function () {
    console.log('feedparser meta event');
});

feedparser.on('readable', function() {
    var chunk;
    while(chunk = this.read()) {
        console.log('feedparser readable event');
    }
});

feedparser.on('drain', function() {
    console.log('feedparser drain event');
})

readableStream.pipe(feedparser);

// feedparser meta event
// feedparser readable event
// feedparser readable event
// feedparser readable event
// feedparser readable event
// readableStream end event
// feedparser finish event
// feedparser end event
GabrielBB commented 7 years ago

What's the difference between "end" and"finish" events?

danmactough commented 7 years ago

@GabrielBB

rdbcci commented 7 years ago

@GabrielBB, @danmactough The difference between "end" and"finish" events is further discussed in this section of node.js doc. Events: 'finish' and 'end'

GabrielBB commented 7 years ago

@danmactough @rdbcci Thanks. I want to save the items in my database after all of them have been pushed to an array in the "readable" event callback. Based on that documentation i guess the "finish" event callback is the right place to do that 👍