Stream writable.writableObjectMode Property in Node.js
×


Stream writable.writableObjectMode Property in Node.js

186

The writable.writableObjectMode property in Node.js reveals whether a writable stream has been configured to work in object mode. In object mode, streams handle JavaScript values other than just buffers or strings — like numbers, booleans, or even objects.

What Is writableObjectMode?

This property returns a Boolean value. If it's true, the stream can write arbitrary JavaScript values. If false, only buffers and strings are allowed. It’s a read-only property that helps you inspect the internal mode of a stream.

Syntax

writable.writableObjectMode

This is a simple property that doesn’t require any arguments. You can use it to check if a stream supports object writing.

Why Use Object Mode?

Object mode is useful when working with non-binary data — for example, streaming structured data like JSON objects or database rows. It allows streams to pass along full JavaScript objects rather than converting them to strings or buffers.

Example: Checking writableObjectMode

Here's a quick demonstration of how to check whether a stream is in object mode:


const { Writable } = require('stream');

const objStream = new Writable({
    objectMode: true,
    write(chunk, encoding, callback) {
        console.log('Received object:', chunk);
        callback();
    }
});

console.log('Is object mode?', objStream.writableObjectMode); // true
    

In this example, since objectMode is set to true, the stream is able to accept full JavaScript objects.

Use Cases

  • Streaming API responses in object form
  • Passing structured data between processes
  • Transforming or filtering data objects in a pipeline

Important Points

  • This is a read-only property — you must set object mode during stream creation.
  • Attempting to write objects to a non-object mode stream will throw an error.
  • Object mode applies separately to readable and writable sides in duplex streams.

Conclusion

The writable.writableObjectMode property in Node.js is a simple yet valuable indicator that helps determine whether a writable stream can handle JavaScript objects. It’s especially useful when designing custom streams that need to work with complex data types beyond just text or binary data.



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!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat