Home > Software engineering >  How to update data on React frontend that are yielded in Flask backend by generator?
How to update data on React frontend that are yielded in Flask backend by generator?

Time:06-01

When a client hits one of my Flask endpoints, I want to construct a python generator that yields a new image every ~5 seconds and sends the image to the React frontend to be updated.

What is the right way to do this? How can I have React update the displayed image?

CodePudding user response:

Have you considered creating a WebSocket connection between the frontend and backend? The socket can remain open and the server can be configured to send the new image in a time interval. On the frontend, you can store the image URL using React.useState and update it in a function that listens for new messages from the WebSocket.

These may be helpful:

https://flask-socketio.readthedocs.io/en/latest/

https://socket.io/docs/v4/client-api/

  • Related