The proper way to create WASM browser service workers.
This crate provides rust library and JS glue code to compose browser service worker on WASI.
Why specifically WASI?
It seems that code compiled to wasm32-wasi target is executing about 2 times faster than code compiled to other wasm32 targets with web bindings. It makes sense to use it for CPU intensive workloads.
Why might I need wasi-worker?
WASM code which executes as part of web application occupies same javascript thread, hence if wasm code is running complex calculations it will block browser application while working. To make it working in separate thread we can employ browser service workers.
As it stated before code compiled to WASI seems to run about 2 times faster (link to benchmark). The only problem is that WASI is not built to be executed from browser, rather it is standard which aims to run WASM code on server side, hence it is missing proper JavaScript bindings. Thankfully to beautiful wasmer-js this crate provides browser service worker WASI runtime as well as communication bridge to/from web application.
Usage example
This example to operate requires JavaScript glue code or WASI environment with properly preconfigured filesystem
use *;
;
// Function will be called from JS on incoming message
pub extern "C"
JavaScript glue code
Is provided with wasiworker tool which can be installed cargo:
cargo install wasiworker
wasiworker will build worker bin target and deploy it with JS glue code under ./dist:
wasiworker deploy
JS glue code source code can be located in the same repository.
More detailed example
use *;
// This function will be called from worker.js on new message
// To operate it requires JS glue - see wasi-worker-cli
// Note: It will be substituted by poll_oneoff,
// though currently poll_oneoff does not transfer control
pub extern "C"
TODO
- library code with WASI fs interface
- basic example
- documentation
- CLI for worker setup