richardson_tm February 2016

API Request Loop Node.js and LiveChat API

As a side project at work, I am currently working on implementing a LiveChat Report web app. Basically, it will download all of our LiveChat data to .csv files for analysis, etc.

I have the entire thing working quite well; however, I am having a hard time wrapping my head around how to loop though requests based on pages of results returned. For example, I want certain information from each chat. LiveChat API only returns 25 chats on each page. Unfortunately, I cannot call each page at a time and depending on the date range parameters, the number of pages varies each time. I want to get all of these pages on 1 csv, if possible.

My request looks like:

function chatListReport() {
document.getElementById('chat_list_submit').addEventListener('click', function(event) {
var req = new XMLHttpRequest();
var params = {date_from:null,date_to:null, page:null}; 

params.date_from = document.getElementById('date_from').value;
params.date_to = document.getElementById('date_to').value;
params.page = document.getElementById('page').value;

req.onreadystatechange = function() {  //when response received
    if (req.readyState == 4 && req.status == 200) {
      var response = (req.responseText);
      var final = "data:text/csv;charset=utf-8," + encodeURI(response);
      var link = document.createElement('a');
      link.setAttribute('href', final);
      link.setAttribute('download', 'ChatListSurveyReport.csv');
      link.click();

     }}
req.open('POST', '/chat_list_report', true);   //submit update via POST to server
req.setRequestHeader('Content-Type', 'application/json');  //set request header
req.send(JSON.stringify(params));
event.preventDefault();
});

My server side looks like this (using NPM liveChatAPI):

app.post('/chat_list_report', function(req, res){
var params = req.body;
api.chats.list(params, function(data){
  var headers = 'Chat Date,Agent,PostChat Survey Rating,Comments';
  var resu        

Answers


LiveChatAdam February 2016

We see two possible solutions:

1: A. Front-end sends a request to the backend, and then the backend returns ID of a job, which the front-end must store on it’s side, for example in the localStorage.

B. At this time, back-end works on the jobs on its own, and tries to finish it, as a complete task.

C. Front-end sends a request to the back-end every few seconds about the task, giving it’s ID. While the task is not complete, back-end replies with 'in-progress', and front-end continues to enquire about the task until its finished.

D. If the front-end enquires about the task, and on this time receive notification that the task is completed, then back-end returns a message like finished' with further completed information.

E. GUI should notify: o Waiting for response o Response successful, link to download a file.

---------OR----------

2: A. Front-end sends request to back-end, back-end registers the task and after completing the task sends an e-mail with the completed data.

B. Front-end informs that the task has been added to the ‘queue’ and only when it is completed will an email be sent with the results of the task.

C. Requires adding additional input on the front end side : e-mail and handling some sort of API for e-mails e.g e,g,:https://postmarkapp.com/

Let me know how it works,

Cheers, Adam

Post Status

Asked in February 2016
Viewed 1,583 times
Voted 9
Answered 1 times

Search




Leave an answer