Node.js http2session.destroy() Method

Last Updated : 22 Aug, 2026

The http2session.destroy() method in Node.js is used to immediately terminate an HTTP/2 session. It can be used when the session needs to be closed because of an error or another connection-related condition.

  • It forcefully closes the HTTP/2 session and its underlying connection.
  • It can optionally specify an error for why the session is being destroyed.
  • It can also provide a callback to be executed after the session is destroyed.

Syntax:

http2session.destroy(error, code, callback)

Where,

  • destroy(): A method that immediately terminates the HTTP/2 session.
  • error: An optional Error object describing the reason for destroying the session.
  • code: An optional HTTP/2 error code associated with the session termination.
  • callback: An optional function that is called after the session is destroyed.

Return Value: The method returns the http2session object.

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

   // Getting session object
   // by using session method
   const http2session = stream.session;

   // Getting alpnProtocol of this session
   // by using alpnProtocol method
   const alpnProtocol = http2session.alpnProtocol;

   stream.end("session protocol : " + alpnProtocol);

   // If any error occur
   http2session.on('error', (code) => {
     console.log(code);
   })

   // Callback for close event
   http2session.on('close', () => {
     console.log("session is destroyed");
   })

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

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: session protocol : h2c
client destroyed
server destroyed
session is destroyed

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 session object
   // by using session method
   const http2session = stream.session;

   // Destroying session 
   // by using destroy() method
   http2session.destroy();

   console.log("session is closed");

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

session is closed
server destroyed

Use Cases of http2session.destroy() Method:

  • It is used to terminate an HTTP/2 session immediately.
  • It is used to handle connection errors or unexpected session conditions.
  • It is used when a session must be forcefully closed instead of being shut down gracefully.

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

Comment

Explore