Run Julia apps almost as-is in the browser, on CPU and GPU!

Turns out that now, with trimming in Julia and WasmCodegen.jl, it is easy to run Julia apps in the browser fully client-side, without any server, on both CPU and GPU!

I had a few Julia tools I wrote and always wanted to make more accessible to people, so I tried browser compilation recently. It turned out totally feasible to take a bunch of packages that know nothing about the browser, add a bit of glue code (really a small browser-specific handler), and have a single codebase run the same way natively and in the browser.

In my experience, the browser run is somewhat slower, often within a factor of two or so, and this can potentially be improved with better WebAssembly compilation going forward. The total download is a few megabytes, and it works in all modern browsers, including on phones.

Here are two apps I built this way.

Synchray demo

A relativistic jet rendered in real time, specifically intended as a demo of WebAssembly and WebGPU: Synchray demo (source: GitHub - aplavin/SynchrayDemo.jl · GitHub)

Key packages used:

  • Synchray.jl: pure Julia physics rendering engine, knows nothing about GPU or web
  • imgui (CImGui.jl, ImPlot.jl, …): lightweight UI library
  • WasmCodegen.jl (@gbaraldi @pankgeorg @asinghvi17 and other contributors): compiles Julia code to run in the browser, as WebAssembly
  • WasmPages.jl (mine): tools to compile C libraries with Emscripten_jll, and build multi-module apps; a big chunk is a stopgap until BinaryBuilder (hopefully!) supports the WebAssembly platform
  • WebGPU.jl (mine): compiles Julia code to run on WebGPU, in the same manner as GPUCompiler.jl’s CUDA/Metal/… backends do it

The repo is a good template to start from: julia --project=web web/build.jl builds a static page that can be served from any host.
Replace Synchray.jl with your computational backend, write some basic UI, and your web app is done :slight_smile:

VLBInspect

The second one is an actually useful one: an in-browser tool for inspecting VLBI interferometric data and making quick images: VLBI explorer. It has a synthetic data button if you just want to look around.

Requirements and rough edges

The only real requirement is that the code you compile to the browser is type stable and completely statically determined, so you need to take some more care when writing packages and code. For these apps I had to make some minor changes to the packages involved, but it is still the same codebase run natively and in the browser. This only applies to the parts that need to run in the browser; functionality not used in these apps can stay as is, even if it is completely type unstable.

Compiling C libraries for the browser is the part that would benefit most from upstream support, as mentioned above.

So that’s what I wanted to share. I know there are other initiatives for compiling Julia to WebAssembly; this is the one I found easiest to use and adapt. Comments, suggestions, or maybe even better approaches are very welcome.
Happy to chat about how to streamline this further, so that for many typical use cases there is no need to write anything browser-specific at all :slight_smile:

Visiting Synchray demo with Safari, it gave

Could not start: CompileError: WebAssembly.Module doesn’t parse at byte 5866: Memory64 is not enabled

but with Chrome, it worked.

Yes, indeed, it requires memory64 – supported on most, but apparent not all, modern browsers:


The reason is that struct layout bytes differ when compiling C libraries for 32 or 64 bits. Here I use imgui through CImGui.jl, and that Julia wrapper package hardcodes some byte offsets in structs for 64-bit version. I really value reuse of existing packages, so decided to go ahead with memory64 in WASM. I heard from a friend of mine that a Safari beta supports this already btw.

Nice work!
CPU 4% usage and GPU 40% on my machine :slight_smile:

  1. What is the size of binary bundle and how long it takes to load first time?
  2. Is it completely sandboxed on client side, or it can access some local resources like files on user PC?
  3. How would you split such an app into client-side code and server-side (to make heavy computations, write into databases, access shared resources etc.)?

Downloads ~2 Mb (with browser/server compression), unpacks to ~5 Mb, for either of the apps. At this point I think the size is totally fine for any reasonable usecase!
Loads in a fraction of a second after that, I’d say…

Same as a regular web page! Fully sandboxed (barring browser vulnerabilities), the only way to access local files is to open a file/folder selection dialog of the browser. That’s what VLBI explorer does – click “open” and it shows browser file selection window.

What you can do easily is write client and server part as two completely different julia projects. The server side with HTTP.jl doing regular server things, and the client side compiled to webassembly doing things in browser (including regular requests to the server). Of course they can share package dependencies, so you might want to have the bulk of code in the shared core so that client/server cannot drift.

Funny that I specifically wanted to make a client side app for heavier computations (the VLBInspect one), because it’s useful but I dont want to run everyone’s compute on my machine :slight_smile: