Home > database >  How can I send a response multiple times during the code execution without sending all the responses
How can I send a response multiple times during the code execution without sending all the responses

Time:10-27

Hello, I've been trying to figure this out since a while but am not able to find a solution for it in node.js

I usually code in Python and I was able to make a dynamically updating API using generators

@app.route("/api/login")
def getQR():
    def generate_output():
        time.sleep(3)
        yield render_template_string(f"Hello 1\n")
        time.sleep(10)
        yield render_template_string(f"Hello 2\n")
    return Response(stream_with_context(generate_output()))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

The above code sends the first response "Hello 1" after 3 seconds then waits 10 seconds and adds "Hello 2" to the response. I'm trying to recreate this in Node.js but can't figure out how to do so. Here is my current code:

const server = http.createServer((req, res) => {
  if (req.url == "/" || req.url == "") {
          res.write("Hello 1\n");
          // Some code execution for 10s
          res.write("Hello 2\n")
          res.end();
  }
});
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

The above code waits for 10 seconds and then sends "Hello 1\nHello 2\n" I'm basically trying to achieve what I could do in the Python code using generators. Any workarounds/solutions or suggestions would be appreciated. Thank you!

CodePudding user response:

Streaming Contents with python

According to python doc, generator is for data streaming:

Sometimes you want to send an enormous amount of data to the client, much more than you want to keep in memory. When you are generating the data on the fly though, how do you send that back to the client without the roundtrip to the filesystem?

The answer is by using generators and direct responses.

Check these references:

Streaming Contents with nodejs

Understanding that what you need is data streaming to your browser for big files transfer, not the classic res.send("hello"), basically the flow is:

  • create a stream commonly for files, not for strings
  • pipe or write it to the response
  • end the exchange

Here an example:

import {createReadStream} from 'fs';
import express from 'express';
const app = express();

app.get('/', (req, res) => {
  var readStream = createReadStream('./data.txt');
  readStream.on('data', (data) => {
    res.write(data);
  });
  readStream.on('end', (data) => {
    res.status(200).send();
  });
});
app.listen(3000);

Check these references:

CodePudding user response:

We will create a time.sleep() alternative in js. First create a function that would call setTimeout.

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

Use await to call this function. Learn more about await here.

await sleep(3000) // waits for 3 seconds
  • Related