Brainstorming ways to share code between elm and elixir

I have been brainstorming ways to share code between elm and elixir, might use it at work. Think some simple calculations like calculating price based on float input and some constants. Some options:

  • Rust, wasm, rustler
  • Gleam
  • Elixir / Erlang compile to js (haven’t found an active project)
  • Implement small DSL that serializes to json and interpret in each language
  • Use python (an existing language used in our stack elsewhere), ffi to python from elixir; brython or other python to js compiler
  • Don’t use shared language, just use a data driven test set in both languages to guarantee equivalent functionality

Other ideas? Most viable?

I am guessing rust is most viable.

I mean why you would use a native interface if elm is used for frontend anyway, once you go over the network there is no reason to use anything beside https.

1 Like

I assume you are suggesting making a network call to the backend to invoke the logic. I wanted something that could respond quickly (as checkboxes are clicked and unclicked, in a long form), so I didn’t keep “call the elixir backend” as an option for this case.

You never mentioned where the infrastructure will be located, if you plan on making a local application where you embed elixir and elm on same machine, then using some of your options might be possible (even though they don’t make much sense, there will be no visible performance gain).

If you need an active communication channel between server and client, then just use websockets. There is the phoenix_channels library that abstracts a messaging interface and luckily there is a client in elm also (if that doesn’t work well you can always use the official JS client), however at this point I would just use liveview as it already does what you want to implement from scratch.

Why do you want to share code? Share data! Use livejson!


I am trying an approach to use Gleam compiled to JS for FE and re-use it in a Phoenix app on the backend.
see: Regretting/Questioning my current stack Flutter + Elixir as solo dev for mobile app - #21 by marcin

1 Like

Let’s say Amazon us east data center for backend, and California for client browser. Want library function calls (or network calls) to complete within 500 ms. I guess a websocket could support that need. We already use phoenix channels through elm on some pages.

After websocket, gleam was my favorite choice, but I figured rust is more mature at this moment.

Liveview isn’t really viable for a single page amongst dozens when you have a lot already implemented in an elm SPA.

So you will be using Rust on WASM, right? Is there some FE framework you’d use to manipulate the DOM ?

Maybe ElixirScript would be useful, but I don’t know if it guarantees consistency between calculations done in JS in a browser and an Elixir backend. (It also hasn’t had a commit since 2019)

1 Like

It would just be pure functions like calculations with floats, so no DOM considerations needed currently.

AFAIK javascript works with floats just like any other dynamically typed language. It has problems with integers though

We ended up duplicating the code between elixir and elm and generating an elm test suite based on the elixir test suite to get assurance about equivalence. Will pursue phoenix channel next time

1 Like