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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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');
});
</code>
<code>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'); }); </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"percentage": 0,
"message": "1 / 84102 deleted"
}
</code>
<code>{ "percentage": 0, "message": "1 / 84102 deleted" } </code>
{
  "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 a DELETE
  • 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.

3

You can write to the network stream in chunks. Call Response.write() each time a packet is available.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>app.delete('/long-operation', (req, res) => {
/* •••Rest of code••• */
// Send Status Update to Client
<b>res.write(/*update*/)</b>
/* •••Rest of code••• */
res.end()
})</code>
<code>app.delete('/long-operation', (req, res) => { /* •••Rest of code••• */ // Send Status Update to Client <b>res.write(/*update*/)</b> /* •••Rest of code••• */ res.end() })</code>
app.delete('/long-operation', (req, res) => {
  /* •••Rest of code••• */
  // Send Status Update to Client
  res.write(/*update*/)
  /* •••Rest of code••• */
  res.end()
})

If no header is set, a header will be chosen implicitly based on the content of the body of the stream. On a Web Client that implements the Web API fetch(), this will be available through the body property of the Response object the Promise resolves with which will be a ReadableStream.

It is pretty simple, network data is sent as streams so that large and small packets can easily be parsed. You see such ideas in Server Sent Events, Bidirectional Streams (e.g., WebSocket), et cetera.

An Express Response is an enhanced version of NodeJS HTTP Response and it supports all built-in fields and methods.


You can use this to implement Server Sent Events (set the right headers), Web Notifications, et cetera. Just as long as this should be a one way stream, this is enough. But if this should be a two way stream? We are now looking at a fundamentally different architectural requirement. That is where WebSocket excel.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật