How do you efficiently sent data from JavaScript to Rust continuesly?

MDN has docs on the raw JavaScript usage of WASM, along with guides on building the WASM module (spoilers, eventually they're going to tell you to use npm to build the JavaScript side). It's a handy start if you want to build the JavaScript side yourself, so you know what is actually getting transferred and when, but it's quite a journey to get to anything actually useful and usable.

IIRC, Rust wasm modules export a single Memory object as memory, and you can directly call any #[no_mangle] pub fn (don't use #[wasm_bindgen] since that needs specific JS on the other side) taking and returning only 32 and 64 bit number types (eg u32, f64, ...).

With those two things and the basic JavaScript API, you can build anything you like, by passing pointers as indexes into memory, exporting memory allocation functions, copying data in and out, and creating reference tables and index encoding schemes to handle preserving object identities so you can call methods, etc.. it's a lot of work!

For an example of transferring data the other way, you might take a look at this project I was playing with a while ago, particularly this WIP branch . The WASM side fills RGB framebuffers and has the JS side copy them out to a canvas. For maximum efficiency, it skips wasm-bindgen entirely in favor of lower-level primitives.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.