0

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.

Andre Frexio
  • 155
  • 1
  • 10
  • 1
    Is you sails.js api doing something complicated with your data? Or are you trying to generalize a question in order solve a different problem. I ask because a simple database save will probably take less time than the journey of the request itself. – Meeker Feb 09 '15 at 15:06
  • You didnt get my question. I need a way to stream data to the frontend. Thats it. Do you know how to do it in sailsJS – Andre Frexio Feb 10 '15 at 03:11
  • 1
    yes, but your question does not provide any usable information for which to provide an example. Sails uses express on top of node. You can simply use response.write() as a method. But you need an event that will trigger this and were not even sure if this is the correct tool for your application. – Meeker Feb 10 '15 at 04:55
  • Example - 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. – Andre Frexio Feb 10 '15 at 10:06
  • Sails comes with WebSockets - You can go as realtime as it gets. What you require is even included in the core of Sails.js - see the [docs](http://sailsjs.org/#/documentation/reference/websockets/resourceful-pubsub). – Robert Rossmann Feb 10 '15 at 16:44

1 Answers1

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();

}
Meeker
  • 5,979
  • 2
  • 20
  • 38
  • All good now, But the request I send using Jquery does not get this message till the last moment, – Andre Frexio Feb 10 '15 at 15:55
  • jQquery just spit everything out when the server send the res.ok() Please help – Andre Frexio Feb 10 '15 at 15:56
  • The thing is when i make the GET request using the web browser link in the address bar,, i an see the server responses showing up on the screen. But I cannot get the same thing to happen with the jQuery GET function. Please help – Andre Frexio Feb 10 '15 at 16:02
  • http://stackoverflow.com/questions/28436353/read-server-streaming-data-using-jquery – Andre Frexio Feb 10 '15 at 16:11