Home > Net >  What is the best way to store previous response from an API and compare with new response in Nodejs?
What is the best way to store previous response from an API and compare with new response in Nodejs?

Time:12-15

I know this question might get downvoted or blocked due the question framing and lack of context but I'm unable to find a term how to handle it.

Anyways here's the problem- I have a database whose values are exposed via a microservice API to Nodejs server, further utilised by reactjs UI. In the Nodejs middleware, I perform some operations and see whether the db API respone values are eligible to be sent to UI or not. Now in UI I'm building a dashboard which will show a "Latest" indicator to those values which the user sees now for first time. How can I work on this logic in UI or middleware?

1 approach I've tried is- Send a flag from middleware that isIndicator= true or not. I use moment library and see if the day value added in db and the current day, the difference between them is <5(it should be displayed only for 5 days), then set flag as true else false.

const d1=moment ().utc()
const d2=moment (dateAdded).utc()
const d=d1.diff(d2);

But this will fail if the value in db is there for long and recently is eligible for UI display.

Can someone please help me build the logic?

CodePudding user response:

If you do not need to store this information on your server for any other use case, and you can tolerate the "latest" indicator showing up per-browser (i.e. it shows up once on desktop Chrome, another time on a mobile browser, for the same data), then one option is for clients to keep track of their last viewed timestamp in the browser's localStorage:

// Get the last timestamp viewed by the user, or 0 if the user hasn't
// viewed anything yet.
const lastViewedTimestamp = Number(localStorage['lastViewedTimestamp'] || 0);
// Assuming your node server returns data in JSON format,
// fetch the data as JSON.
const response = await fetch('/your/data').then(response => response.json());
// Parse the response timestamp into UTC milliseconds since epoch, if needed.
const currentTimestamp = moment(response.date).utc().milliseconds();
// Show the "latest" indicator if the data is newer than the last data viewed
// by the user.
const isLatest = currentTimestamp > lastViewedTimestamp;
// Persist the last viewed timestamp to localStorage.
localStorage['lastViewedTimestamp'] = currentTimestamp;

Then, if users refresh the page, or even exit the browser and then re-open the browser, they should not see the "latest" indicator as long as the returned data is not newer than the last data that was fetched.

More on localStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

CodePudding user response:

Breaking news scenario

Following your sample :

some stories are marked as Breaking news, after sometime it disappears

You need to define what turns a story into a "breaking news".

Here some samples:

  • User Likes frequency
  • Average reading time
  • How long does the user stay on the story?
  • How many times has it been shared?

If you achieve to store these metadata of a single story in your database, it would be easy to develop an endpoint in your api called /api/news/breaking which returns you the breaking news. After some days, the same endpoint will return another news which accomplish the requirements.

In this approach, you don't need to compare responses. You just need to store metadata of your stories and its interactions with the user.

Your scenario

show a "Latest" indicator to those values which the user sees now for first time

If user with id jane_doe enter to the react web and needs to access to some entities which are in the database, using your api, it would be easy to store that in your database:

  • jane_doe > first attempt to retrieve entities

If the same user, needs to retrieve the same entity, with the previous data, you are capable of determine if these entities were already showed to the user.

In this case, comparison between responses are not required.

  • Related