Node.js http2stream.state Method

Last Updated : 8 Sep, 2026

The http2stream.state provides miscellaneous information about the current state of an Http2Stream. It contains details about the stream's flow-control window and whether the stream has been closed locally or remotely.

  • It provides information about the current state of the HTTP/2 stream.
  • It includes details related to the stream's flow-control window.
  • It indicates whether the stream has been closed locally or by the remote peer.

Syntax

http2stream.state

Where,

  • http2stream: An instance of the Http2Stream class that represents an HTTP/2 communication stream.
  • state: A property that provides an object containing information about the current stream state.

Return Value: The property returns an object containing information about the current state of the Http2Stream.

Example 1:

javascript
// Node.js program to demonstrate the
// Http2Stream.state method

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 ');

  // Getting all information of 
  // this http2stream object
  // by using state method
  const status = stream.state;

  stream.end("priority weight : " 
        + status.weight);

  // Stopping the server
  // by using the close() method
  server.close(() => {
    console.log("server destroyed");
  })
});

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 destroyed");
  })
});

Output:

status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed

Example 2:

javascript
// Node.js program to demonstrate the
// Http2Stream.state method

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 all information of 
   // this http2stream object
   // by using state method
   const status = stream.state;

   stream.end(
     "The sum weight of all Http2Stream : " 
     + status.sumDependencyWeight);

   // Stopping the server
   // by using the close() method
   server.close(() => {
     console.log("server destroyed");
   })
});

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 destroyed");
  })
});

Output:

Received: The sum weight of all Http2Stream : 0
client destroyed
server destroyed

Use Cases of http2stream.state Method:

  • It is used to inspect the current state of an HTTP/2 stream.
  • It is used to monitor the stream's flow-control information.
  • It is used to determine whether the stream has been closed locally or remotely.

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

Comment

Explore