The http2session.close() method in Node.js is used to gracefully close an HTTP/2 session. It allows the session to finish processing existing streams before the connection is closed.
- It initiates a graceful shutdown of the HTTP/2 session.
- It allows existing streams to complete before closing the session.
- It can be used for both client and server HTTP/2 sessions.
Syntax:
http2session.close(callback)Where,
- http2session: An instance of the
ClientHttp2SessionorServerHttp2Sessionclass. - close(): A method that initiates the graceful closing of the HTTP/2 session.
- callback: An optional function that is called when the session is closed.
Return Value: The method returns the http2session object.
Example 1:
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;
// Getting alpnProtocol of this session
// by using alpnProtocol method
const alpnProtocol = http2session.alpnProtocol;
stream.end("session protocol : " + alpnProtocol);
http2session.close(() => {
console.log("session is closed");
})
// 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:
server destroyed
session is closed
status : 200
Received: hello
Received: session protocol : h2c
Example 2:
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 alpnProtocol of this session
// by using alpnProtocol method
const alpnProtocol = http2session.alpnProtocol;
stream.end("session protocol : " + alpnProtocol);
http2session.close(() => {
console.log("session is closed");
})
// 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 protocol : h2c
client destroyed
server destroyed
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2session_close_callback