Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Building Apps with Tauri and Elixir

Posted on Oct 19 • Originally published at crabnebula.dev For the longest time, building desktop apps was a daunting task to web developers. That is, until technologies like Electron made creating these apps more approachable to a wider audience. Today, we’ve got a wide array of native applications built with solutions like Electron, Tauri, Capacitor, and many more. While these are great solutions, sometimes configuration can be tricky and the applications we create can become somewhat bloated in terms of memory usage.The Elixir programming language is no stranger to desktop applications as the language actually supports building them out of the box. It uses wxWidgets: a C++ library that lets developers create applications for Windows, macOS, Linux and other platforms with a single code base. But wxWidgets has a very complex API, and doesn’t solve issues that usually come with desktop applications around packaging.Thus, we set out to build a desktop Application using a LiveView from the Phoenix Framework in Elixir. For the uninitiated, a LiveView is a process that receives events, updates its state, and renders updates to a page as diffs. The LiveView programming model is declarative: instead of saying “once event X happens, change Y on the page”, events in LiveView are regular messages which may cause changes to its state.The end goal we were working towards was:An easy development cycle.A lightweight and snappy application.First-class bundling support.From the moment we discovered Tauri, we really felt like this was the perfect fit. The API is really solid, the configuration files are minimal and easy to understand, and the usage of Rust makes it way easier to add new functionalities and think about interesting ways of interoperating with Elixir via the Rustler library.The biggest benefits we derived from Tauri were Wry and the sidecar mechanism. Wry (the second half of Tauri: tao/wry) is a cross-platform WebView rendering library in Rust that supports all major desktop platforms like Windows, macOS, and Linux. It essentially spins up a native web view from whatever operating system it’s running on and doesn’t require an application to bundle one with it. Wry greatly reduces the overhead of “pushing” a browser to our users, instead leaning on the host OS to handle rendering a web view. This made our applications really lean.With Tauri apps, you may need to bundle dependent binaries with your application to make your application work or prevent users from installing additional dependencies (e.g., Node.js or Python). We call these dependent binaries sidecars. Sidecars are a beautiful solution that enabled us to fix quite a few issues regarding the lifecycle of complex applications since it allowed us to bundle multiple executables into our solution.Due to sidecars, we are able to have a Rust application that handles the window and the communication with the OS and a separate Elixir application that handles the business logic and the communication with the Rust application via Tauri’s sidecar mechanism.Since we’re using a Phoenix project, we already have a full folder structure to use which means that we had to find a proper way to install Tauri and use it in a project which is trickier than it seems since we want to install it scoped to our project only.Fortunately the Elixir community already had a similar issue with Tailwind so we used the same methodology where we download the required dependencies into our Phoenix _build folder and run from there.Knowing all of this, we automated the full setup by doing the following steps:Installing tauri-cli using cargo into the _build folderRun tauri init with multiple options setupWriting our own cargo.toml fileSet the expected application nameChange dependencies to use crates.io instead of folders such is the default behavior of tauri initWriting our main.rs to write the code required to:Start the sidecarCheck if sidecar is connectedStart TauriChange the Tauri config files toSetup Sidecar external binarySetup allowList required to run our sidecarThis was essentially the work required to add Elixir interop to Tauri projects. It required a small set of changes and we were able to use the full power of the Tauri CLI and a Tauri project that we could completely change at will.Now came one of the biggest questions: How can we properly sidecar a Phoenix application?The answer was given by the Elixir community with burrito which enables users to pack up everything an Elixir application needs within a binary namely Zig Archiver to package the binary and Zig Wrapper that wraps the Erlang Virtual Machine to be used in multiple platforms (Zig + Rust in the same project 🤯).With burrito, a user just needs to run two commands to fully build a desktop application using Elixir and Tauri:mix ex_tauri.install which installs tauri-cli only for this project and creates all the files mentioned abovemix ex_tauri dev: which passes through the commands to our installed Tauri processAnd to bundle it up you would just need mix ex_tauri build and it’s done!This was all in a new library that is still work in progress but very promising. We are eager to get contributions from the community. Learn more at ex_tauri on GitHub.Currently the fact that we weren’t able to use the binary directly means that we need to install the tauri-cli using cargo which is non-optimal and increases time to first screen by a lot. Additionally the fact that some options are trickier or not available to set via CLI forces us to rewrite some files, for example we can’t set sidecar information.Another tradeoff is the need to have a communication line between Rust and Elixir, that is still largely undecided but we might look into Rustler as a potential route or use a socket (similar to Docker).Finally, there’s a tricky element in the need to fully compile and package Elixir all the time, even in dev mode. However, this also feels like a solvable problem with a bit more time and effort.Truly Tauri offers what, in our opinion, is the strongest case at the moment to build desktop applications independent of the use case. The fact that with a couple of days of work and some research and help from the Tauri team there’s a library to build desktop applications that can be bundled up securely with just a couple of commands shows how versatile Tauri is and how approachable it is to build native applications regardless of the stack.We’ve used Elixir with the Phoenix Framework because it’s our preferred stack, but the methodology we used could be used with any language and with relatively low effort. We can’t wait to work more with Tauri since we’re still just scratching the surface of what this project is capable of.Author: Filipe Cabaço, Software Developer at SupabaseTemplates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Philip John Basile - Jun 9 Tasko Olevski - Jun 10 Tsiry Sandratraina - Jun 10 Adolfo Neto - Jun 9 Once suspended, crabnebula will not be able to comment or publish posts until their suspension is removed. Once unsuspended, crabnebula will be able to comment and publish posts again. Once unpublished, all posts by crabnebula will become hidden and only accessible to themselves. If crabnebula is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to CrabNebula. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag crabnebula: crabnebula consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging crabnebula will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Building Apps with Tauri and Elixir

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×