Node.js http2session.state is a property that provides information about the current internal state of an HTTP/2 session. It is useful for monitoring session behavior and understanding how the connection is performing at runtime.
- It provides information such as the number of active streams and pending settings.
- It helps developers monitor and debug HTTP/2 session activity.
- It is accessed from an active
Http2Sessioninstance.
Syntax:
http2session.stateWhere
- http2session : It represents an active
Http2Sessioninstance in Node.js. - state : It is a property of the session that provides an object describing the current internal state.
Return Value: It returns an object containing the current HTTP/2 session state.
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 ');
const http2session = stream.session;
// Getting state associated with this
// session by using state method
const status = http2session.state;
stream.end("local window size : "
+ status.localWindowSize);
// 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: local window size : 65535
client localSettings
server localSettings
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 state associated with this
// session by using state method
const status = http2session.state;
stream.end("numeric id of recent data : "
+ status.lastProcStreamID);
// 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: numeric id of recent data : 1
client destroyed
server destroyed
Use Cases of http2session.state Method
- It is used to monitor the current state of an HTTP/2 session.
- It helps in debugging session-related issues.
- It is useful for tracking stream activity and session metrics.
- It assists in managing and optimizing HTTP/2 connections.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2session_state