This can be a duplicate question but cant find any solution out there. I already tried to search it on this fast hours but I am out of luck.
My question is how I can send stream(idk if stream is the right term) of json objects to the client.
Example I am reading data on the DB, and on every 5 records it found I will send the data to the client in json format. Then read the data on the client using Angular httpclient lib.
I already tried expressjs response.write()
, but the client was unable to receive the data until I call response.end()
.
What is the proper way of doing this?
I was thinking about socket.io but that would be overkill.
Edit
Other exmple
function getData(res: Response, req: Request):void {
for(let i = 0; i<100; i++{
res.write(JSON.stringify({data:1}) + '\n');
}
res.end(); // There is data received on the browser until this was called.
}
Or something like this. I was expecting a data will reach every 1 second but they came up at the same time after 10s.
function getData(res, req){
const s = setInterval(()=>{
res.write(JSON.stringify({value: 'test value'}));
}, 1000);
setTimeout(()=>{
clearInterval(s);
res.end();
}, 10000);
}