#Best practices for dividing logic between FE and BE?

1 messages · Page 1 of 1 (latest)

patent locust
#

My program makes a http call every x seconds and displays the formatted results in a table. Currently, the loop that makes the http call lives in the backend code and it emits the response as an event that the FE listens to. The loop is started and stopped by commands from the FE. The time between calls is a setting of my program and it's managed by Tauri manages as state (with commands for the FE to get and set it).

I did it like this because I thought it would be best to put as much logic as possible in the BE code but I wonder now if this is really the best approach. For example, if I want to display a "time since last update" in the UI I need to either create a new event so that the BE send this info to the FE (messy) or let the FE compute this based on the time since the last event (indirect). The alternative would be to put the update loop and the state management in the FE and have the BE only expose commands for IO. Most FE frameworks have some sort of state management solution so this might be easier to work with because it reduces the need for state synchronization.

In my case I think either option would be good enough but it makes me wonder more generally what best practices are for dividing logic between FE and BE code.

frail sand
#

What I do for things like this is indeed use the frameworks state management system, but I don't keep the truth of the state in the frontend, I just synchronize it with the backend

Exactly how you set this up is up to you, but I tend to use events that send to the frontend when the state in the BE has been updated so that the FE can get a payload to update its state

#

Handled correctly your frontend will be fully reactive to changes made in the BE, and you don't need a billion functions and events going back and forth all over the place 🙂

patent locust
#

Is there a specific reason why you make the BE state the source of truth? I do backend development for my day job so it makes sense to me intuitively but when both are running on the same device I'm not sure the same rules still apply.