Node.js http2.bufferSize Method

Last Updated : 10 Aug, 2026

The http2.bufferSize property in Node.js indicates how much data is currently queued in memory for an HTTP/2 stream but not yet sent. It is useful for understanding data flow and managing backpressure in streaming scenarios.

  • It shows the amount of pending data in the buffer.
  • It helps monitor stream performance and flow control.
  • It can be used to detect slow consumers or network delays.
  • A higher value can indicate potential bottlenecks.

Syntax:

stream.bufferSize

Where :

  • stream: Represents an HTTP/2 stream like Http2Stream or Http2ServerResponse.
  • bufferSize: Indicates how many bytes are currently buffered and waiting to be sent.

Return Value: The number of characters currently buffered to be written.

Example 1:

JavaScript
const http2 = require('http2');
const fs = require('fs');

// Private key and public certificate for access
const options = {
  key: fs.readFileSync('private-key.pem'),
  cert: fs.readFileSync('public-cert.pem'),
};

// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);

server.on('stream', (stream, requestHeaders) => {
  stream.respond({ 
    ':status': 200, 
    'content-type': 'text/plain' 
  });

  stream.write('hello ');
  stream.end('world');

  // Getting buffer size
  // by using stream.bufferSize method
  const value = stream.bufferSize;

  console.log("buffer size : " + value);
  
  // Stopping the server
  // by using the close() method
  server.close(() => {
    console.log("server closed");
  })
});

server.listen(8000);

// Creating and initializing client
// by using tls.connect() method
const client = http2.connect(
    'http://localhost:8000');

const req = client.request(
  { ':method': 'GET', ':path': '/' });

req.on('response', (responseHeaders) => {  
  console.log("status : " 
  + responseHeaders[":status"]);
});

req.on('data', (data) => {
  console.log('Received: %s ',
  data.toString().replace(/(\n)/gm, ""));
});

req.on('end', () => {
  client.close(() => {
    console.log("client closed");
  })
});

Output:

buffer size : 17
status : 200
Received: hello
Received: world
client closed
server closed

Example 2:

JavaScript
const http2 = require('http2');
const fs = require('fs');

// Private key and public certificate for access
const options = {
  key: fs.readFileSync('private-key.pem'),
  cert: fs.readFileSync('public-cert.pem'),
};

// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);

server.on('stream', (stream, requestHeaders) => {
  // Getting buffer size
  // by using stream.bufferSize method
  const value = stream.bufferSize;

  stream.end("buffer size : " + value);
  
  // Stopping the server
  // by using the close() method
  server.close(() => {
    console.log("server closed");
  })
});

server.listen(8000);

// Creating and initializing client
// by using tls.connect() method
const client = http2.connect(
  'http://localhost:8000');

const req = client.request(
  { ':method': 'GET', ':path': '/' });

req.on('data', (data) => {
  console.log('Received: %s ',
  data.toString().replace(/(\n)/gm, ""));
});

req.on('end', () => {
  client.close(() => {
    console.log("client closed");
  })
});

Output:

Received: buffer size : 0
client closed
server closed

Use Cases of http2.bufferSize Method

  • It helps monitor buffer buildup during data streaming.
  • It can be used to detect backpressure in HTTP/2 streams.
  • It assists in identifying slow clients or network delays.
  • It helps optimize performance in real-time applications.

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2stream_buffersize

Comment

Explore