Live data from Hacker News

Ask HN: What are modern architecture patterns for desktop applications?

news.ycombinator.com

11–20 of 25 posts

Re: Ask HN: What are modern architecture patterns for desktop applications?

#11
post #6

On Linux, you want to check D-Bus [1] if you need IPC (but that is primarily used for message exchange between different desktop apps). Everything else you mentioned sounds like a recipe for bloat. Desktop GUI libraries (like Qt or Gtk) ship everything you'll need and you can get a platform-agnostic app without much of a problem. > running multithreaded and writing into a sqlite database. sqlite doesn't support multi…

SQLite does support multiple writers, they take turns:

> SQLite only supports one writer at a time per database file. But in most cases, a write transaction only takes milliseconds and so multiple writers can simply take turns. SQLite will handle more write concurrency than many people suspect. Nevertheless, client/server database systems, because they have a long-running server process at hand to coordinate access, can usually handle far more write concurrency than SQLite ever will.

Source: https://www.sqlite.org/whentouse.html#:~:text=multiple%20wri...

Re: Ask HN: What are modern architecture patterns for desktop applications?

#12
It's not uncommon to split front end and back end logic into modules. I've seen lots of projects where the business logic is even shared between a suite of applications.

But it's very unusual to split it over an RPC/IPC boundary - it's usually just linked in.

Xi had specific design goals of allowing multiple independently developed front ends. There are use-cases for this type of development but it's largely not worth the effort (YAGNI).

The typical application architecture is going to depend on your use-cases. I.e a db app is going structured different from a game or a CAD program.

Re: Ask HN: What are modern architecture patterns for desktop applications?

#14
If it were me, I'd try for a single process with the UI and the indexer running in separate threads.

Communication between threads could be as simple as global variables + a mutex lock.

Two problems to solve:

- Communication from UI thread to backend thread

- Communication from backend thread to UI thread

For communicating to the backend thread, I like using event flags but other people like using mailboxes. Both work. With event flags, you set a bit (for instance, backend_thread.start = True) and if the backend thread is sleeping, it gets woken up by the OS scheduler.

UI frameworks often provide a function to run your code in the UI thread, so your indexer could use that to periodically update the UI. For example you could have an Update() function that reads the global variables defining the state of the indexer and updates every UI element to match, and then the indexer periodically schedules Update to run on the main thread when it's doing its work.

In my work there's typically a state machine that coordinates UI + backend. It might start with state = BACKEND_STOPPED. Then when you press the start button, transitions to state = BACKEND_RUNNING and notifies the backend thread using an event flag. When the backend thread finishes, it can transition back to BACKEND_STOPPED and schedule an update to the UI.

----

Another pattern I've seen is using the database for communication. You might have a "jobs" table in sqlite with indexing jobs to perform. The front-end adds "jobs" to the table and notifies the backend thread to wake up. The back-end processes jobs and updates their status and notifies the UI thread when the status changes.

Re: Ask HN: What are modern architecture patterns for desktop applications?

#15
i dont see why u need separation of backend/frontend... anyhow. i guesse u can make a frontend executable. On boot it checks if the backend is running or not. if not. run it. Then establish a form of communication. usually sockets. if communication is needed 1 direction, just keep a read() open on the backend, if communication is needed 2 directions, u will have to keep a read() open on both ends.

But again, separating front & backend for a desktop application, is kinda weird.

Re: Ask HN: What are modern architecture patterns for desktop applications?

#16
I use Electron for DataStation [0]. Electron has builtin message passing between the UI layer and the main process. I built a small wrapper around this in TypeScript so I could have the messages be typechecked.

Within the main process I actually have it call a Go process to handle all the heavy lifting that goes on in DataStation like database queries, HTTP requests, etc. Communication between the Nodejs main process and the Go process just happens in temp files. All long-term data is stored in SQLite.

Happy to talk more about it if you want! My email is in my profile.

[0] https://github.com/multiprocessio/datastation

Re: Ask HN: What are modern architecture patterns for desktop applications?

#18
post #2

I'd look to mobile applications for inspiration, as they have many of the same constraints as desktop applications do. In the mobile space, MVVM is pretty popular.

Having started in modern frontend js, and now doing mostly .Net 6 API dev with a Xamarin client, I don't quite understand the obsession with MVVM. MVU seems clearer and more intuitive by far, but that could just be my web background talking.

Edit: to give an example of what I'm talking about, see this article's visualization of the unidirectional data flow you get with MVU. This feature in particular makes MVU much less ambiguous than MVVM (IMO at least)

https://thomasbandt.com/model-view-update

Re: Ask HN: What are modern architecture patterns for desktop applications?

#20

> From what I understand most applications also separate frontend and backend layers definitely not on the desktop, no

Well, you do but usually within the same process.

I usually don’t because DearImgui exists and I usually do simulation stuff which benefits from the immediate mode approach.

Post reply on HN