How to implement a writable stream

0 votes

I want to pipe data from an amazon kinesis stream to a an s3 log or a bunyan log.

The sample works with a file write stream or stdout. How would I implmeny my own writable stream?

//this works
var file = fs.createWriteStream('my.log')
kinesisSource.pipe(file)

this doesn't work saying it has no method 'on'

var stream = {}; //process.stdout works however
stream.writable = true;
stream.write =function(data){
    console.log(data);
};
kinesisSource.pipe(stream);

what methods do I have to implement for my own custom writable stream, the docs seem to indicate I need to implement 'write' and not 'on'

Oct 13, 2020 in Node-js by kartik
• 37,510 points
1,031 views

1 answer to this question.

0 votes

Hello @kartik,

to create a writeable stream is quite simple. Here's is the example:

var fs = require('fs');
var Stream = require('stream');

var ws = new Stream;
ws.writable = true;
ws.bytes = 0;

ws.write = function(buf) {
   ws.bytes += buf.length;
}

ws.end = function(buf) {
   if(arguments.length) ws.write(buf);
   ws.writable = false;

   console.log('bytes length: ' + ws.bytes);
}

fs.createReadStream('file path').pipe(ws);

Hope it help!!

Thank you!!

answered Oct 13, 2020 by Niroj
• 82,880 points

Related Questions In Node-js

0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try: var fs = require('fs'); var dir = ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
5,732 views
0 votes
1 answer

How to print a stack trace in Node.js?

Hello @kartik, To print stacktrace of Error in console in ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
626 views
0 votes
0 answers

How to append to a file in Node?

I am trying to append a string to a ...READ MORE

Jul 9, 2020 in Node-js by kartik
• 37,510 points
768 views
0 votes
1 answer

How to Install a local module using npm?

Hello @kartik, This is what worked for me: npm ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
8,815 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,719 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
2,927 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,880 points
836 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
774 views
0 votes
1 answer

How to save a stream into multiple destinations with Gulp.js?

Hello @kartik, Currently you have to use two ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,880 points
1,305 views
0 votes
1 answer

How to install a previous exact version of a NPM package?

Hello @kartik, If you have to install an ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
2,571 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP