Member-only story
How to stream data in ExpressJs?
Not a medium member: See here
Streaming is a very popular terminology in Software Development in which a chunk is sent at a time instead of whole response. In other domains, it is still very used like a stream of flowing river, waterfall because a particular amount of water is flowing instead of the whole volume of river at a time.
You have used Chatgpt right ? It sends data in small amount instead of whole response as compared to Gemini. Also in youtube, netflix or any video streaming platform, the video is getting streamed instead of whole video to be downloaded. Streaming is used because it is more efficient and a piece of data is readily available instead of waiting for the whole data and taking actions based on the chunk of data especially in case of healthcare or stock market analysis.
We can achieve the same in ExpressJs by showing an example by creating an API which utilizes the Readable
and Writeable
streams with artificial delay so that we can see the delays between the chunk instead of an illusion which represents a full response.
Here is an example:
const express = require('express');
const cors = require('cors');
const app = express();
const chunks = [
'Microsoft', 'Windows', 'is', 'a', 'product', 'line', 'of', 'proprietary', 'graphical', 'operating', 'systems'…