node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »



Node.js Transform stream that passes data through at n bytes per second and can be paused.

This module offers a Throttle passthrough stream class, it allows you to write data to it and it will be passed through in n bytes per second. It is also possible to pause and resume the steam. This can be used to send PCM Data to a speaker module in realtime.


$ npm install advanced-throttle


Here's an example of throttling a file readstream at 80 bytes per second and outputting the data to stdout:

var Throttle = require('advanced-throttle');
var fs       = require('fs');
// create a "Throttle" instance that reads at 80 bytes per second 
var throttle = new Throttle(80);
var in = fs.createReadStream('data.txt');
// initialize the break after 80 bytes 
setTimeout(function() {
}, 1000);
// after 80 bytes wait 1 second for the user to read the text 
function wait() {
    if (throttle.isStreamPaused()) {
    } else {
    // either wait 1 second for the 80 bytes to be written or give the user the opportunity to read the text. 
    setTimeout(function() {
    }, 1000);


## Throttle()

The Throttle passthrough stream class is very similar to the node core stream.Passthrough stream, except that you specify a bps "bytes per second" option and data will not be passed through faster than the byte value you specify.

You can invoke with just a bps Number and get the rest of the default options. This should be more common:

process.stdin.pipe(new Throttle(100 * 1024)).pipe(process.stdout);

Or you can pass an options Object in, with a bps value specified along with other options:

var t = new Throttle({ bps: 100 * 1024, chunkSize: 100, highWaterMark: 500 });

### .pauseStream() : void Pauses the stream but keeps storing data in the internal buffer.

### .resumeStream() : void Resumes the stream which will cause all stored data to be written out at the throttled speed.

### .isStreamPaused() : boolean Returns whether the stream is currently paused or running.