3

Scenario - data changes on some of the tables in the database, say using a file upload from another process, like an ETL tool that is not part of the react-redux application. How can we then refresh the react-redux application components? Is web sockets the only to push such a change from server to client? Or do we refresh the affected components at regular intervals? but then all such ports will have to be left open all the time? Backend is a java/oracle application. Thanks

user1908591
  • 33
  • 1
  • 3

2 Answers2

1

You should make an Ajax call (I like using axios) to the database every time the relevant React component loads to ensure that the data is up to date. If you are creating the component with React.createClass, use getInitialState() inside said component.

EDIT: Since you don't want the component to query the DB every time it loads but rather only after a specified time period, I suggest a variation on the code below:

getInitialState() {
  //Define a variable and set as the current date object
  let date = new Date;

  //Convert the variable's value to a string
  date = date.toString();

  //Slice the day of the month from the date string
  let day = date.slice(6,8);

  //Query the database if it's the 15th or 28th (approximately every 2 
  //weeks, accounting for February
  if (day == ("15" || "28")) {
    //Ajax call here
    return {data: ajaxResponse};
  }
}

Please note that this code will query the DB every time the component loads on the days selected. Since the date object goes down to the millisecond, you could make the time window as narrow as you want. Just keep in mind that if the component is never loaded during that time, the DB will not be queried until the if conditional is satisfied again.

223seneca
  • 1,136
  • 3
  • 19
  • 47
  • But I was looking to see if there is any way to not be making a DB call every time the component mounts - this will be a chatty application and didn't want to add more to the back and forth communication. As the data load may only happen on a weekly basis or once in 2 weeks – user1908591 Feb 17 '18 at 04:03
  • I've updated the answer based on your comment above. – 223seneca Feb 17 '18 at 15:19
  • Thanks! I like this idea. Could tweak it further to get a date from the DB itself to see if new data needs to be pulled in - it can be a combo of date and flag I guess. The DB date/flag will need to be updated when background data refresh process runs. – user1908591 Feb 17 '18 at 16:07
1

The actual implementation of this state sync is up to you to decide and analyze which solution is the best to your problem. You can choose between fetching that state at regular intervals or have the server pushing that state into the client, you can achieve the same user experience with different implementation.

About the implementation details, the only thing that is really necessary in your Redux application is creating a corresponding state for what you want, and exposing actions (in that case, pure and synchronous actions) to change that state. With those actions, and with the dispatch method from the store, you can have your task of sync (which can be running in parallel) dispatching those actions to update your state as soon as it has data to sync. Furthermore, you can implement your own state change validation so the task only dispatches an action when it's really necessary.

To implement a parallel task which can sync your client, you can use redux-saga for example, and build a saga which in regular intervals fetch that state and updates the Redux store. You can use redux-saga with the other pushing data solutions too. To push data to the client, you can use a WebSocket, or more complete solutions like socket.io or Firebase Cloud Messaging.

rodgobbi
  • 1,442
  • 14
  • 10
  • Thanks for the ideas. Have been looking at socket.io. FCM is ruled out as no cloud solutions are allowed on this client. Using parallel tasks in redux-saga I think will work better than checking on individual component level. The saga event will have to be tied at the higher level components - which will always be rendered versus individual ones which may or may not rendered during the time slot. – user1908591 Feb 17 '18 at 16:26
  • I'm using `redux-saga` and definitely recommend it. You can for example create actions to request specific data, and have many components dispatching those actions to then have sagas watching those actions and deciding if it's necessary to fetch data or update the store or just use what is cached, that way you maintain a centralized logic and the components as simple as possible. – rodgobbi Feb 17 '18 at 19:41
  • that's a good idea given the complexity of the application we will be building. – user1908591 Feb 17 '18 at 20:15