SailsJS stream status from backend to frontend

泪湿孤枕 提交于 2019-12-25 04:06:58

问题


In my webapp I use SailsJS backend to fetch data from an API (facebook graph api) and that fetched data will be saved on the database on the backend when it has finished fetched.

So my question is when I make a request to the backend to do all this work how do I show the user how much of it has been completed. At the moment all I can show is a loading icon. But I like to show something like 1% completed, 89% completed as such

a sailsjs app create 1000 records on the backend. Right. and when every record has been created i need to know while im on the frontend. You know what I mean ? I need a response to the web browser saying 1/1000 record has been created

Please help me here if you are familiar with the framework

Cheers.


回答1:


Essentially you can loop through your record create in some fashion then use response.write() to stream updates to the user. This is a crude example:

function doStuff(req,res){

// You can use response.write() to do what you need.
var processedRecords = 0;
var totalRecords = 1000;

res.status(200);
while (processRecords < totalRecords){

    Model.create( --create model stuff-- , function(){
       processedRecords++;
       res.write('Finished ' + processedRecords + 'of ' + totalRecords);
     })

  }
  return res.end();

}


来源:https://stackoverflow.com/questions/28396292/sailsjs-stream-status-from-backend-to-frontend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!