Node.js Http2Stream close Event

Last Updated : 8 Sep, 2026

The 'close' event in Node.js is emitted when an Http2Stream is destroyed. It indicates that the stream has been closed and can no longer be used.

  • The event is emitted when the Http2Stream is destroyed.
  • It can occur when the stream is closed locally or by the connected peer.
  • The HTTP/2 error code associated with the closure can be checked using http2stream.rstCode.

Syntax

http2stream.on('close', callback)

Where,

  • http2stream: An instance of the Http2Stream class.
  • 'close': An event that is emitted when the stream is destroyed.
  • callback: A function that is executed when the close event occurs.

Return Value: The event does not return any value.

Example 1:

JavaScript
// Node.js program to demonstrate the
// close Event API
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 rst stream code
   // by using rstCode api
   const status = stream.rstCode;

   stream.end("RST_STREAM error code : "
     + status);

   stream.destroy();

   // Close event is emitted
   stream.on('close',()=>{
     console.log("stream is destroyed")
   })

  // 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:

stream is destroyed
status : 200
Received: hello
client destroyed
server destroyed

Example 2:

JavaScript
// Node.js program to demonstrate the
// close Event
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 rst stream code
   // by using rstCode api
   const status = stream.rstCode;

   stream.end("RST_STREAM error code : " + status);

   stream.destroy();

   // Close event is emitted
   stream.on('close',()=>{
     console.log("stream is destroyed")
   })
  
  // 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:

stream is destroyed
Received: RST_STREAM error code : 0
client destroyed
server destroyed

Use Cases of Http2Stream close Event

  • It is used to detect when an HTTP/2 stream has been closed.
  • It is used to perform cleanup tasks after a stream is destroyed.
  • It is used to monitor the lifecycle of an HTTP/2 stream.

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

Comment

Explore