Node.js Http2ServerResponse.socket Method

Last Updated : 4 Aug, 2026

The Http2ServerResponse.socket property in Node.js returns the underlying socket associated with an HTTP/2 response. It provides access to the network connection used to communicate with the client.

  • It provides access to connection details.
  • It is useful for advanced networking tasks.
  • It is a read-only property.

Syntax:

response.socket

Where:

  • response: The Http2ServerResponse object that represents the HTTP/2 response.
  • socket: A property that returns the underlying network socket used for the connection.

Return Value: It returns the net.Socket object, which represents the network connection between the server and the client.

Example 1:

JavaScript
// Node.js program to demonstrate the
// Http2ServerResponse.socket method
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'),
};

// Request and response handler
const http2Handlers = (request, response) => {

    // Getting tls socket reference by 
    // using response.socket method
    const value = response.socket;

    // Display the display
    console.log("byte written value of socket :- " +
    value.bytesWritten)
};

// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options, http2Handlers);

server.on('stream', (stream, requestheader) => {
    stream.write('hello ');

    // Getting all information of this http2stream object
    // by using state method
    const status = stream.state;

    stream.end("priority weight : " + status.weight);

    // 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', (responsesocket) => {
    console.log("status : " + responsesocket[":status"]);
});

req.on('data', (data) => {
    console.log('Received: %s ',
    data.toString().replace(/(\n)/gm, ""));
});

req.on('end', () => {
    client.close(() => {
        console.log("client destroyed");
    })
});

Output:

byte written value of socket :- 9
status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed

Example 2:

JavaScript
// Node.js program to demonstrate the
// Http2ServerResponse.socket method
const http2 = require('http2');
const fs = require('fs');
const { RSA_NO_PADDING } = require('constants');

// Private key and public certificate for access
const options = {
    key: fs.readFileSync('private-key.pem'),
    cert: fs.readFileSync('public-cert.pem'),
};

// Request and response handler
const http2Handlers = (request, response) => {

    // Getting tls socket reference by 
    // using response.socket method
    const value = response.socket;

    // Display the display
    response.end("byte written value of socket :- "
            + value.bytesWritten)
};

// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options, http2Handlers);

server.on('stream', (stream, requestHeaders) => {

    // Getting all information of this http2stream object
    // by using state method
    const status = stream.state;

    stream.end("The sum weight of all Http2Stream : " + 
    status.sumDependencyWeight);

    // 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': '/www.geeksforgeeks.org' });

req.on('data', (data) => {
    console.log('Received: %s ',
    data.toString().replace(/(\n)/gm, ""));
});

req.on('end', () => {
    client.close(() => {
        console.log("client destroyed");
    })
});

Output:

Received: byte written value of socket :- 9
client destroyed
server destroyed

Use Cases of Http2ServerResponse.socket Method

  • It is used to access information about the client connection.
  • It is used to inspect the underlying network socket.
  • It is useful for advanced connection handling and debugging.
  • It is used when low-level socket operations are required.

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

Comment

Explore