0

I have a program written where the client side is in JavaScript (React) and the server side is written in Python using Flask. I want the client side to refresh data every 30 seconds but I want the server side to refresh at different paces (i want Route A to refresh every 30 seconds, Route B to refresh every 60 seconds) By Route I am referring to Python-Flask Routes.

So far in my client side (as far as this refreshing code goes) I have

refreshData() {
        request.get("/cData")
        .end((err,res) => {
            if (err) {
                console.log("error cdata request: " + err);
                return;
            }
            this.setState({cTimes: res.body});
        });

        request.get("/mData")
        .end((err,res) => {
            if (err) {
                console.log("error mdata request: " + err);
                return;
            }
            this.setState({mTimes: res.body})
        });
    }
    componentDidMount() {
        this.refreshData();
        setInterval(this.refreshData(), 30000);
    }

A route in the backend looks like this

@app.route("/cData")
def cData():
   #do work
   return jsonify(work)

The return data of each flask route will be in JSON form (dictionary)

Any idea what I need to do on the client side (React) to really get this done? Rather than using the threading module in Python (since that does not allow multiple threads to run at the same time) is there a different way? Multi-processing?

thatdevop
  • 819
  • 2
  • 8
  • 19
  • Just update the data when a new response comes in and the cached data is older than 30/60 secs, otherwise return the cached data – Jonas Wilms Feb 12 '18 at 16:49
  • @JonasW. how do I check when a new response comes in? A new response isn't sent out automatically, i more so have to "refresh" the Flask Route to see if there's an update or not. – thatdevop Feb 12 '18 at 17:13
  • `def cData():` <- that seems like a function to me that is called when someone requests the server? Oh, by the way i meant `request` and not `response` .. :/ – Jonas Wilms Feb 12 '18 at 17:16
  • @JonasW. that is correct but maybe i didn't explain this right. I meant i need the processes to send out requests (like cData) in 30 second intervals or 60 second intervals. Which is what i'm confused with? How do I have it automatically have requests in time-intervals? Sorry if that doesn't make sense. – thatdevop Feb 12 '18 at 17:54
  • Why fo you need that? If there is no client request, why do you need the updated data? – Jonas Wilms Feb 12 '18 at 20:40
  • well that's what i'm asking. i don't know if it's clear or not. But i'm not exactly sure how to send requests from react, then relate it to Flask in Python. so yes I want to send the request, but i only want cData to be requested every 30 seconds and mData requested every 60 seconds. how do i do that? @JonasW. – thatdevop Feb 12 '18 at 21:46

1 Answers1

0

You could try to use setInterval command you can look on w3school how to use it or you can try (this i recommand) to use flask -socketio in your project and with flask socetio you can transfer data in real time between client and server

Bogdan Bibina
  • 110
  • 3
  • 12
  • I'll take a look into flask -socketio, that seems kind of like what I want to do. As far as setInterval goes, would I be able to have it call "routes" based on a time-interval? That is where my confusion mainly comes, the ability to call routes and then refresh data that is being shown with new data. – thatdevop Feb 19 '18 at 15:14
  • actually using flask -socketio is not an option for me right now. Using setInterval is nice, but I want to do this server side (so using Python in Flask). from what i'm reading, setInterval is not a function BUT there is a work-around..here: https://stackoverflow.com/questions/2697039/python-equivalent-of-setinterval What i'm worried about is what if the server and the client request at the same time, will the threading in there break the process? Is there a way I could do this except with Multi-processing maybe? – thatdevop Feb 19 '18 at 15:57