Yaswant Kumar S’ Post

🎬 𝘖𝘯𝘦 𝘤𝘰𝘯𝘤𝘦𝘱𝘵. 𝘍𝘰𝘶𝘳 𝘧𝘰𝘳𝘮𝘴. 𝘐𝘯𝘧𝘪𝘯𝘪𝘵𝘦 𝘶𝘴𝘦 𝘤𝘢𝘴𝘦𝘴. You've seen what Streams are. Now let's meet each one properly. ━━━━━━━━━━━━━━━ 🧵 Node.js Streams 𝗧𝗵𝗲 𝟰 𝗙𝘂𝗻𝗱𝗮𝗺𝗲𝗻𝘁𝗮𝗹 𝗦𝘁𝗿𝗲𝗮𝗺𝘀 𝗘𝘅𝗽𝗹𝗮𝗶𝗻𝗲𝗱 ━━━━━━━━━━━━━━━ 1️⃣ 𝗥𝗲𝗮𝗱𝗮𝗯𝗹𝗲 𝗦𝘁𝗿𝗲𝗮𝗺 Data flows IN. You consume it chunk by chunk. Example → fs.createReadStream() Key events: → 𝗱𝗮𝘁𝗮 — fires every time a chunk arrives → 𝗲𝗻𝗱 — fires when there's nothing left to read → 𝗲𝗿𝗿𝗼𝗿 — fires if something goes wrong 2️⃣ 𝗪𝗿𝗶𝘁𝗮𝗯𝗹𝗲 𝗦𝘁𝗿𝗲𝗮𝗺 Data flows OUT. You push it somewhere. Example → fs.createWriteStream() Key functions: → 𝘄𝗿𝗶𝘁𝗲() — sends a chunk to the destination → 𝗲𝗻𝗱() — signals no more data will be written → 𝗳𝗶𝗻𝗶𝘀𝗵 event — fires when all data has been flushed 3️⃣ 𝗗𝘂𝗽𝗹𝗲𝘅 𝗦𝘁𝗿𝗲𝗮𝗺 Both Readable AND Writable at the same time. Example → net.Socket (TCP connections) A socket can receive data AND send data simultaneously. Two lanes. One stream. ⚡ 4️⃣ 𝗧𝗿𝗮𝗻𝘀𝗳𝗼𝗿𝗺 𝗦𝘁𝗿𝗲𝗮𝗺 Data comes in → gets modified → goes out. Example → zlib.createGzip() Reads raw data, compresses it on the fly, outputs compressed chunks. No need to load the full file. No memory spike. 🔥 Key function: → 𝗽𝗶𝗽𝗲() — chains streams together like a pipeline ━━━━━━━━━━━━━━━ 𝗧𝗵𝗲 𝗯𝗶𝗴 𝗽𝗶𝗰𝘁𝘂𝗿𝗲: ━━━━━━━━━━━━━━━ Readable → Transform → Writable That's a full streaming pipeline. Read a file → compress it → write it to disk. All in one chain. All chunk by chunk. All production grade. 🚀 Which of the 4 have you used in your projects? Drop it below. 👇 #NodeJS #BackendDevelopment #JavaScript #Streams #Developer

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories