[Rust] — Tauri-rs An Experience

Last week, i wanted to write a desktop application for learning dictionary. After a couple of hours researching, i came across Tauri-rs a…

[Rust] — Tauri-rs An Experience

Last week, i wanted to write a desktop application for learning dictionary. After a couple of hours researching, i came across Tauri-rs a framework written in Rust which uses webview as the front-end.

If you’ve dabbled with C# Windows Forms before, then you’ll notice that Tauri and Windows Forms share some similarities. The project structure of Tauri consists of two main parts:

  • First, there’s the UI built using WebView. You have the freedom to write this part in various languages like vanilla JS, Typescript, ReactJS, Vite, VueJS, or even Wasm Yew-rs. The beauty of this approach is that you can spice up your app with CSS and HTML instead of relying on clunky “drag and drop” or “XML” components like in WPF.
  • And then there’s the real muscle of the app, handled by good ol’ Rust code. It takes care of all the heavy lifting behind the scenes, ensuring your app runs smoothly and efficiently.
Src is the Front-end, src-tauri is the Back-end here

Communication between 2 parts

Tauri uses a particular style of Inter-Process Communication called Asynchronous Message Passing. It contains 2 IPC primitives — Events and Commands

Events

Events are fire-and-forget, one-way IPC messages that are best suited to communicate lifecycle events and state changes. Unlike Commands, Events can be emitted by both the Frontend and the Tauri Core.

Commands

Tauri also provides a foreign function interface-like abstraction on top of IPC messages1. The primary API, invoke, is similar to the browser's fetch API and allows the Frontend to invoke Rust functions, pass arguments, and receive data.

Because this mechanism uses a JSON-RPC like protocol under the hood to serialize requests and responses, all arguments and return data must be serializable to JSON.

Build and deployment

When it comes to deploying applications on multiple platforms, cross-platform compatibility can be a real challenge. But fear not! Tauri comes to the rescue with its powerful Github Action called Tauri Actions, designed specifically for hassle-free cross-OS distribution. With Tauri Actions, you can effortlessly deploy your app across different operating systems, making the whole build process a breeze. Say goodbye to platform dilemmas and hello to seamless cross-platform deployment with Tauri!

Setting up the build is a trivial task. By creating a Github workflfow with a yaml file, you can build and release the app for 3 major OS: Windows, Linux and MacOS, it even builds the AppImage to support RPM-based Linux distros.

Embedding external files and resources

When it comes to including external files and resources in your app during the build process, it can be a real headache. Dealing with the path resolution for those resources can be an even bigger pain in the neck.

Tauri supports embedding additional files by allowing user to tweak the configuration files

Accessing the file is really straight forward

Downside

One major drawback I discovered is the significant memory consumption when running and debugging the application. While running npm + rust with file watcher generally works smoothly, there are instances where it can be a bit sluggish.

The hot reload feature upon saving files can be occasionally slow, likely because rust-analyzer and npm have to put in extra effort during each reload.

The deployment process may be a bit slow due to Rust’s slightly longer compilation time. This is because Rust prioritizes runtime safety, sacrificing some speed during compilation. While a 10-minute pipeline is not an issue for my specific use case, larger companies aiming for continuous delivery of new features may experience a notable slowdown in their processes.

Conclusion

Having experienced Tauri, I believe it has reached a level of maturity suitable for production. While there are some downsides in the development environment, the combination of Rust’s reliability and the flexibility of HTML and CSS make it well worth the effort when aiming for a stunning UI. Additionally, the support for Typescript and WASM adds an extra layer of awesomeness to the mix.

If you find the post useful, please consider buying me a coffee.