I am implementing an addon in node.js where there is a continuous in flow of data from another application to this addon module.
This addon modules parses the data, populates the data into V8 data structures and calls the callback of node.js application.
Everything works fine till here.
On top this I'm planning to implement a Readable stream, and this is the code for readable stream.
var addon = require('../build/Release/addon');
var inherits = require('util').inherits;
var Readable = require('stream').Readable;
var util = require('util');
module.exports = MyReadableStream;
function MyReadableStream(opts) {
if(!(this instanceof MyReadableStream)) {
return new MyReadableStream(opts);
}
this.addonFunc = addon.DataSource;
Readable.call(this, opts);
}
inherits( MyReadableStream, Readable);
MyReadableStream.prototype._read = function( ) {
var self = this;
this.addonFunc(function(data) {
var ret = self.push(JSON.stringify(record, null, ' '));
}, function(error) {
self.push(err);
}, function(end) {
self.push(null);
});
}
This is the streamReader implementation on top of my addon.
When I bypass the stream reader and consume data directly in the the node.js application, it exits gracefully, but when I read the data using this streamReader
and read in the application using something like
var stream = new MyReadableStream(opts);
stream.on('data', callback)
stream.on('error', callback2)
stream.on('end', callback3)
The node.js application never exits and runs indefinitely.
Any help or suggestions would help me move forward.
Thanks
Gayathri