Node.js http2.connect() Method

Last Updated : 10 Aug, 2026

The http2.connect() method in Node.js creates a client-side HTTP/2 session with a server. It returns an Http2Session object that can be used to send HTTP/2 requests and receive responses.

  • It is a method of the built-in http2 module.
  • It establishes a connection to an HTTP/2 server.
  • It accepts the server URL and optional configuration settings.

Syntax:

http2.connect(authority[, options][, listener])

Where

  • authority: It is the URL representing a remote HTTP/2 server to connect to.
  • options (optional): It can be maxDeflateDynamicTableSize, maxSettings, maxSessionMemory, etc option can be used according to need.
  • listener (optional): It is the one time listener of the 'connect' event.

Return Value: This method returns an Http2Session object that represents the client-side HTTP/2 connection.

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 ');
  stream.end('world');
  
  // Stopping the server
  // by using the close() method
  server.close(() => {
    console.log("server closed");
  })
});

server.listen(8000);

// Creating and initializing client
// by using http2.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 closed");
  })
});

Output:

status : 200
Received: hello
Received: world
client closed
server closed

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) => {
  stream.end('world');
  
  // Stopping the server
  // by using the close() method
  server.close(() => {
    console.log("server closed");
  })
});

server.listen(8000);

// Creating and initializing client
// by using http2.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 closed");
  })
});

Output:

Received: world
client closed
server closed

Use Cases of http2.connect() Method:

  • It is used to create client-side HTTP/2 connections.
  • It is used to send requests to HTTP/2 servers.
  • It is useful for building high-performance network applications.
  • It is commonly used to communicate with secure HTTP/2 APIs.

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

Comment

Explore