Question
Javascript - Express.js - send regularly status responses to the client
i want to implement a logic where my express backend regularly sends updates to the client on how far a process reached at the moment.
For this i've created an example backend:
import express from 'express';
const app = express();
app.delete('/long-operation', (req, res) => {
// Placeholder for the long operation
for (let i = 0; i < 30000; i++) {
setTimeout(() => {}, 1000);
if (i % 100 === 0) {
// Send Status Update to Client
}
}
res.end();
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
For the regular update i thought of this:
{
"percentage": 0,
"message": "1 / 84102 deleted"
}
For the frontend im using Vue2.7 with axios.
I already tried a couple of things like:
- new EventSource() -> this sends a
GET
Request and not aDELETE
- Axios
onDownloadProgress
-> i couldn't get it to work and this is intended for file downloads
I also researched a bit about WebSockets
but this seems a bit overkill for the problem.
I would like to have some kind of recommendation, for how i could do this. Im also open for a better solution if there is any.