The timeout event in Node.js is emitted by an Http2Stream when the stream remains inactive for the configured timeout period.
- The event is emitted when the configured timeout period is reached without stream activity.
- The event can be handled using the
stream.on('timeout', callback)method. - The timeout duration can be configured for the
Http2Stream.
Syntax
http2stream.on('timeout', callback)Where,
- http2stream: An instance of the
Http2Streamclass. - 'timeout': An event that is emitted when the stream has been inactive for the configured duration.
- callback: A function that is executed when the timeout event occurs.
Return Value: The event does not return a value.
Example 1:
// Node.js program to demonstrate the
// timeout event
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) => {
// Setting timeout for the stream
// by using setTimeout()
stream.setTimeout(3000)
// Emitting timeout event
stream.on('timeout',()=>{
stream.respond({
':status': 200,
'content-type': 'text/plain'
});
stream.end("surprise !!!");
})
// 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:
status : 200
Received: surprise !!!
client destroyed
server destroyed
Example 2:
// Node.js program to demonstrate the
// timeout event
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) => {
// Setting timeout for the stream
// by using setTimeout()
stream.setTimeout(3000)
// Emitting timeout event
stream.on('timeout',() => {
console.log("Timeout event called")
})
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
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 closed");
})
});
Output:
Timeout event calledUse Cases of Http2Stream Timeout Event
- It is used to detect periods of inactivity on an HTTP/2 stream.
- It is used to handle streams that remain inactive for too long.
- It is used to perform custom actions when a stream reaches its timeout period.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_event_timeout_1