slawlor / ractor

Rust actor framework
MIT License
1.31k stars 68 forks source link

WebAssembly Support #124

Open tekacs opened 1 year ago

tekacs commented 1 year ago

Is your feature request related to a problem? Please describe. Ractor seems to be very close to capable of running in a WASM environment, even a browser one. It would be amazing to have an actor system that could be used client- and server-side in a Rust app.

Describe the solution you'd like I was able to get ractor running in the browser very very simply and with minimal changes over at https://github.com/tekacs/ractor/tree/wasm-support from master by using the WIP swappable runtimes in concurrency.rs.

The implementation is janky and a little incorrect, but mostly works and does the job nicely. Making this more correct and validating tests would be the next step.

I would love to incorporate such changes into upstream ractor to make it possible to keep going with an approach like this in the longer term.

Describe alternatives you've considered I currently have a custom client-side actor system in my WASM environments... it would be nice to unify things under the banner of ractor.

slawlor commented 1 year ago

This is awesome! I don't think the implementation looks that janky actually, I understand the implementation requirements around JoinHandle but that could be cleaned up relatively easily I think (that's probably quite tokio specific tbh).

Tokio's default I/O channels (tokio::sync::mpsc, etc) are all runtime independent and I'm glad they seem to work with in WASM.

One thing though, it looks like if we enable tracing, we're back onto the tokio runtime? I feel like maybe in WASM it would make sense to just ignore the tracing configuration bit and not spawn anything named (as it's really only a feature for tokio-console to assist identifying which tasks are which actors). Also 1 small nit, I wonder if it'd be better to utilize a shared macro or something if we're going to share implementations (i.e. the select! statement in your wasm_primatives module).

Does everything work as expected? The tests all pass on a WASM runtime? You can also try out the examples and benchmarks to see how it does!

If we clean it up a bit, this would be a great PR :)