Search code examples
pythonreactjsdesign-patternsfastapi

FastAPI send data without request from frontend


I have a frontend (react) which displays the current status of some hardware. The status of the hardware is periodically checked by some python script.

If the current status of the hardware changes i want to trigger some FastAPI call which sends the data from the backend to the frontend.

Currently i am periodically sending a request from the frontend to the backend and ask if the status of the hardware changed. But it feels not like the correct approach (Status of hardware can stay the same for a long time).

Is there any design pattern or solution for my problem?


Solution

  • Such cases are solved by subscribing to events; there are two ways to constantly connect via webSocket, or use SSE (Server Sent Events). Considering that it may be idle for a long time and the state will not change, I would advise using SSE