stream.Writable close Event in Node.js
0 198
Streams in Node.js offer a powerful way to handle reading and writing data. While most developers are familiar with events like 'data'
and 'finish'
, there’s another useful event that’s worth knowing — the 'close'
event. In the context of writable streams, the writable.close
event is triggered when the stream and its underlying resource are fully closed. Let's break down what this event is, when it occurs, and how to use it effectively.
What is the writable.close Event?
The 'close'
event in a writable stream signals that the stream has been closed and its resources (like file descriptors) are released. This doesn’t necessarily mean that the stream was closed gracefully — it could be the result of a manual call to destroy()
or an error.
In short, it's the final step in the lifecycle of a stream, indicating that it’s no longer usable.
When is the 'close' Event Emitted?
This event is emitted after:
- The stream has ended using
writable.end()
, and - All internal resources have been released.
Note that this event might also fire if writable.destroy()
is called or if an error forces the stream to shut down unexpectedly.
Syntax
You can listen for the event using the standard .on()
method:
writableStream.on('close', () => {
console.log('Stream and its resource are now closed.');
});
Difference Between 'finish' and 'close'
The 'finish'
event indicates that all data has been flushed to the destination, and no more data will be written. However, it doesn’t mean the stream is closed. The 'close'
event, on the other hand, confirms that the stream and its underlying resource have both been shut down.
Example: Using the 'close' Event
Here’s a simple example demonstrating the 'close'
event in action with a file stream:
const fs = require('fs');
const writable = fs.createWriteStream('log.txt');
writable.write('Logging first line.\n');
writable.end('Final entry.\n');
writable.on('finish', () => {
console.log('All data has been written.');
});
writable.on('close', () => {
console.log('Stream is closed and resources released.');
});
In this code, both 'finish'
and 'close'
are used to monitor different parts of the stream's lifecycle. First, the stream confirms that writing is complete, and then the resource itself is released.
When Should You Use the 'close' Event?
The 'close'
event is especially useful in the following scenarios:
- Cleaning up temporary files or connections after the stream ends.
- Logging or monitoring stream shutdowns.
- Detecting abnormal terminations triggered by
destroy()
or errors.
Conclusion
The writable.close
event in Node.js is a crucial part of understanding the full lifecycle of a writable stream. It ensures that you can respond not just when writing is complete, but also when the stream has fully shut down and released its resources. Whether you're working with files, sockets, or custom writable streams, listening to this event helps you write more robust and maintainable code.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments