Node.js http2session.destroyed Method

Last Updated : 17 Aug, 2026

The http2session.destroyed property in Node.js indicates whether an HTTP/2 session has been destroyed. It is a boolean property that helps determine the current state of the session.

  • It provides information about the current session state.
  • It is available on both client and server HTTP/2 sessions.
  • It can be checked before performing session-related operations.

Syntax

http2session.destroyed

Where,

  • http2session: An instance of the HTTP/2 ClientHttp2Session or ServerHttp2Session class.
  • destroyed: A property that indicates whether the session has been destroyed.

Return Value:

  • The property returns true if the session has been destroyed.
  • The property returns false if the session has not been destroyed.

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

   const http2session = stream.session;

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

   // Callback for close event
   http2session.on('close', () => {
      
      const status  = http2session.destroyed;

      if(status)
         console.log("session is destroyed");
      else
        console.log("session is not 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;

   // Checking if the session is destroyed or not
   // by using destroyed method
   const status  = http2session.destroyed;

   if(status)
     stream.end("session is destroyed");
   else
     stream.end("session is not 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:

Received: session is not destroyed
client destroyed
server destroyed

Use Cases of http2session.destroyed Method

  • It is used to check whether an HTTP/2 session is still active.
  • It is used to prevent operations on a destroyed session.
  • It is used to monitor the lifecycle of an HTTP/2 session.

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

Comment

Explore