christhekeele

christhekeele

This is a bit of a hare-brained experiment, but I’d like to try using Popcorn for isomorphic Elixir, that is, to run the same Elixir code on the backend and frontend in the same project.

The core problem I am having is that I cannot tell what mix popcorn.cook is actually bundling up and have no visibility into contents of the bundle to verify my setup.

The core symptom I am experiencing is that debug logging tells me popcorn found my bundle.avm on the client, but the Popcorn.init Promise times out (and so therefore must never be finding my code that calls Popcorn.Wasm.register/1).

The setup isn’t trivial to minimally reproduce, and I’m sure whatever I’m doing wrong is not subtle, so I’ll summarize here in hopes of exposing a glaring conceptual misunderstanding. (I am happy to create the not-so-minimal-example anyways, as a submission to Popcorn examples if nothing else, but would rather get things working first before extracting my approach.) Also seeking any feedback on this approach.

  1. Popcorn appears to operate at the Application level of distribution, which makes sense. So I’ve set up an umbrella application with a Phoenix web app and a Popcorn sim app.
  2. In my config.exs I capture the MIX_ENV via config_target() and compile it into my applications’ envs and make it available at runtime on each Application, ex
    def target, do: Application.compile_env!(:sim, :target)
  3. I have popcorn.cook configured to always use :wasm for its target.
  4. I have popcorn configured to emit everything into the web apps’s assets directory.
  5. I require popcorn.js in my app.js bundle and call (async () => Popcorn.init(...))().then(popcorn => console.log(popcorn)) there.
  6. I copy over all of popcorn’s output, including the bundle, into my priv/static/assets to be served by Plug.Static.
  7. I also require the sim application as in_umbrella from my web application and add it to the list of extra_applications so that it starts in the backend as well as on page load.
  8. The sim application just starts a module-based DynamicSupervisor. In the module’s init method, before returning DynamicSupervisor.init(strategy: :one_for_one), I call:
if Simulation.Application.target() == :wasm do
  Popcorn.Wasm.register(__MODULE__)
end

The idea here is that sim gets started both in backend and frontend, and can start a worker with the same code in either place (with the right invocation of JS or Elixir). Simulation.Application.target() can be consulted to navigate any difference in behaviour required for where it is running.

Everything seems to work asset-wise, Popcorn.init is definitely finding my static bundle.avm asset and trying to start it, however the promise always times out, so it seems to not actually have sim correctly bundled and starting inside, or at least Popcorn.Wasm.register(__MODULE__) must never get called. Or I’m fat-fingering awaiting promises somehow, I guess. I have no idea how to debug from here!

I have tried making the register unconditional in case I’m messing up threading along the :wasm target throughout bundling, but the promise still times out so reaching register is unrelated to the target. I’ve tried logging anything in the sim application startup process in hopes of seeing what the target is, or anything at all, and see no console output. Any ideas?

For reference, I am using:

  • Erlang 26.0.2
  • Elixir 1.17.3-otp-26
  • Popcorn github: "software-mansion/popcorn", tag: "v0.1.8.17"

My console.log looks like:

Main: init, params:  {container: undefined, bundlePath: '/assets/js/popcorn/bundle.avm', wasmDir: '/assets/js/popcorn/', debug: true, onStdout: ƒ}
popcorn.js:389

Main: mount, container:  html
popcorn.js:389

Main: init, params:  {container: undefined, bundlePath: '/assets/js/popcorn/bundle.avm', wasmDir: '/assets/js/popcorn/', debug: true, onStdout: ƒ}
popcorn.js:389

Main: mount, container:  <html lang=​"en" data-theme=​"dark">​<head>​…​</head>​<body>​…​</body>​<iframe srcdoc=​"<html>
      <html lang="en" dir="ltr">
          <head>
            <meta name="bundle-path" content="../​/​assets/​js/​popcorn/​bundle.avm" /​>
          </​head>
          <script type="module" src="/​assets/​js/​popcorn/​popcorn.js" defer></​script>
          <script type="module" src="/​assets/​js/​popcorn/​AtomVM.mjs" defer></​script>
          <script type="module" src="/​assets/​js/​popcorn/​popcorn_iframe.js" defer></​script>
          <script type="module" defer>
            import { runIFrame }​ from "/​assets/​js/​popcorn/​popcorn_iframe.js";​
            runIFrame()​;​
          </​script>
      </​html>" style=​"visibility:​ hidden;​ width:​ 0px;​ height:​ 0px;​ border:​ none;​">​…​</iframe>​<iframe srcdoc=​"<html>
      <html lang="en" dir="ltr">
          <head>
            <meta name="bundle-path" content="../​/​assets/​js/​popcorn/​bundle.avm" /​>
          </​head>
          <script type="module" src="/​assets/​js/​popcorn/​popcorn.js" defer></​script>
          <script type="module" src="/​assets/​js/​popcorn/​AtomVM.mjs" defer></​script>
          <script type="module" src="/​assets/​js/​popcorn/​popcorn_iframe.js" defer></​script>
          <script type="module" defer>
            import { runIFrame }​ from "/​assets/​js/​popcorn/​popcorn_iframe.js";​
            runIFrame()​;​
          </​script>
      </​html>" style=​"visibility:​ hidden;​ width:​ 0px;​ height:​ 0px;​ border:​ none;​">​…​</iframe>​</html>​

// Some 30 seconds pass...

Promise timeout
app.ts:17
^ Where I call (async () => Popcorn.init(...))().then(popcorn => console.log(popcorn))

My (relevant) app.ts code looks like:

import { Popcorn } from "./popcorn/popcorn.js";
const popcorn = (async () => Popcorn.init({
    bundlePath: "/assets/js/popcorn/bundle.avm",
    wasmDir: "/assets/js/popcorn/",
    onStdout: console.log,
    debug: true,
}))()
    .then((popcorn) => {
        console.log(popcorn);
        return popcorn;
    })
    .catch((error) => {
        console.log(error);
        return undefined;
    });

Notes:

  • I am setting the right CORS headers to execute WASM on Plug.Static, so that’s not the issue (and I imagine it would manifest itself differently if it was)

Showing Posts 1 to 4

christhekeele

christhekeele OP

There are a few improvements to be made to Popcorn’s APIs to support umbrella applications better I might contribute back, but after digging deep into the machinery of popcorn init and sorting out a few problems I still had, no idea what’s up. The AtomVM is definitely starting in browser with the correct bundled Elixir code, and definitely nothing is happening once it does.

mat-hek

mat-hek

Membrane Core Team

Hi, can you share the repo so I can reproduce? :wink:

christhekeele

christhekeele OP

I’ll work up a repro sometime this week!

christhekeele

christhekeele OP

@mat-hek I lied, it took a month, but here’s my minimal repro setup broken down into bite-size commits: GitHub - christhekeele/isomorphic_popcorn: Repro of problem setting up isomorphic popcorn · GitHub

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews