Offloading work to web workers in deployments using a bundler #3769
-
SummaryIs it possible to use Wasm from inside a web worker when using wasm-bindgen to build a JS + Rust/Wasm app that is deployed via a bundler? Additional DetailsI am working on a mixed JS + Rust/Wasm project that is deployed using Webpack 5. Recently I added a feature that requires a few seconds of computation time from inside Wasm. Currently, I use wasm_bindgen to expose a Rust struct to JS that contains the Rust application state. I call a method on this JS instance which in turn calls the Rust function that performs the calculation. I'd like to offload this computation to a webworker to avoid blocking the UI, possibly by passing the data necessary for the computation to the web worker and then calling a Wasm function inside the web worker. This would mean having the Wasm module instantiated in both the main thread and the worker. I've been researching how to do this for several days, but still am a bit confused about what is and isn't possible. I've tried the following:
Here, however, I don't know how to instantiate the Wasm module inside the worker, again because I think the bundler can effectively place it anywhere. I did try to call
I've read that in the wasm_bindgen guide that
Am I stuck rewriting my deployment to not use a bundler, or is there something I am overlooking? Apologies if I have made a mistake somewhere in my reasoning; I'm relatively new to a lot of the ecosystem around Wasm. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Take this with a grain of salt because I've never used bundlers and I'm unfamiliar with the part of the code in |
Beta Was this translation helpful? Give feedback.
-
Based on help from others here, on the Rust language form, and the demo project in wasm-bindgen-rayon, I can indeed confirm that it is possible and actually straightforward to deploy a JS + Rust/Wasm app with support for WebAssembly threads via a bundler. I made a minimal example here: https://github.com/kmdouglass/playground/tree/main/wasm-threads-bundler @daxpedda I think that the wasm-bindgen guide might be worth updating or amending. As was mentioned in #3720 , there's nothing special about the Do you have any suggestions for where in the docs this information might best go? |
Beta Was this translation helpful? Give feedback.
Based on help from others here, on the Rust language form, and the demo project in wasm-bindgen-rayon, I can indeed confirm that it is possible and actually straightforward to deploy a JS + Rust/Wasm app with support for WebAssembly threads via a bundler. I made a minimal example here: https://github.com/kmdouglass/playground/tree/main/wasm-threads-bundler
@daxpedda I think that the wasm-bindgen guide might be worth updating or amending. As was mentioned in #3720 , there's nothing special about the
web
build target, and the fact that one of the primary targets is namedbundler
can mislead one into thinking that only this target can be used with a bundler.Do you have any suggestions for w…