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

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews