Node.js http2session.connecting Method

Last Updated : 22 Aug, 2026

The http2session.connecting property in Node.js indicates whether an HTTP/2 session is currently in the process of establishing a connection. It is available on Http2Session instances and helps track the connection state.

  • It indicates whether the HTTP/2 session is still being connected.
  • It changes to false before the connect event is emitted.
  • It is primarily relevant while establishing an HTTP/2 client connection.

Syntax:

http2session.connecting

Where,

  • http2session: An instance of the Http2Session class that represents an HTTP/2 connection.
  • connecting: A property that indicates whether the session is still connecting.

Return Value:

  • The property returns true while the session is still connecting.
  • The property returns false after the session has finished connecting.

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;

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

   if(status)
      stream.end("session is connecting")
   else
      stream.end('session is connecting')

  // 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: session is connecting
client destroyed
server 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 this session is connecting 
   // or not by using connecting method
   const status = http2session.connecting;

   if(status)
     stream.end("session is connecting")
   else
     stream.end('session is connecting')

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

status : 200
Received: hello
Received: session is connecting
client destroyed
server destroyed

Use Cases of http2session.connecting Method:

  • It is used to check whether an HTTP/2 session is still being established.
  • It is used to determine whether the connection process has completed.
  • It is used to monitor the connection state before performing session operations.

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

Comment

Explore