Node.js http2session.socket Method

Last Updated : 17 Aug, 2026

The http2session.socket property in Node.js returns the underlying socket associated with an HTTP/2 session. It can be used to access information about the network connection between the client and server.

  • Underlying Socket: It provides access to the network socket used by the HTTP/2 session.
  • Connection Details: It can be used to inspect properties of the underlying connection.
  • HTTP/2 Session: It is available through an Http2Session object created using Node.js HTTP/2 APIs.

Syntax:

http2session.socket

Where

  • http2session: It represents an active HTTP/2 session.
  • socket: It returns the underlying net.Socket associated with the session.

Return Value: It returns the underlying net.Socket associated with the HTTP/2 session.

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;

   // Getting socket associated with this session
   // by using socket method
   const status = http2session.socket;

   stream.end("address family of socket : " 
      + status.address().family);

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

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

Output:

status : 200
Received: hello
Received: address family of socket : IPv6
client localSettings
server localSettings

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;

   // Getting socket associated with this session
   // by using socket method
   const status = http2session.socket;

   stream.end("port address of socket : " 
      + status.address().port);

   // 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: port address of socket : 8000
client destroyed
server destroyed

Use Cases of http2session.socket Method

  • Access Socket Details: It can be used to access properties of the underlying network socket.
  • Monitor Connection: It can help monitor the state of the HTTP/2 network connection.
  • Debug Connections: It can be useful for debugging HTTP/2 connection issues.

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

Comment

Explore