-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support WASM for native zmq in the web browser #64
Comments
I've added |
Update: I've learned a bit more about web sockets vs tcp. Web sockets is a protocol, not a transport (i.e. HTTP vs tcp). It is built on top of TCP. However, you cannot connect a websocket client to a tcp socket unless the server is also using websockets - just like you can't make an HTTP request to a tcp socket unless its actually a HTTP server listening on that socket. This is why the |
I've looked into the ecosystem to support this a bit. First, there is the question of the async executor. Tokio does not support WASM, and doesn't seem to have any plans to do so. async-std currently has paritial support for WASM - importantly, it builds for the platform and task::spawn works (according to here). Its important to note that we technically care only about the executor, not the other functionality of tokio/async-std, but we may consider transitioning as an option. We should try to take care to write our code as runtime-independent as possible. Next, there is the question of how to get a websocket library that works with the async ecosystem. The best option looks to be async-tungstenite, which can work across either tokio or async-std We also need to consider how to address the lack of certain transport types on WASM. As previously stated, tcp simply won't exist on wasm - pretty much the only transport mechanism will be |
@Alexei-Kornienko would you be open to a PR to get the code to work on both async-std and tokio, via a feature flag? My hope is we can design the code to be mostly runtime-agnostic, and the bits that will actually need one or the other can use either depending on the flag. It doesn't look like |
@TheButlah In theory I'm open for such contributions. However I'm not sure that it will be easy to implement. You can take a look at codec code. We'll have to implement something like Codec and Framed for async-std |
@Alexei-Kornienko looking into it, the Codec and Framed stuff shouldn't really depend on any particular async runtime - it just needs common traits from I found https://crates.io/crates/futures_codec, which looks to have pretty low development velocity, but the crate is small (about 1k lines of code) and seems feature complete. I think it ought to be feasible to use that crate in lieu of Once we do that, I'm sure we can abstract over tokio and async-stds other concepts and try to rely exclusively on |
@TheButlah I don't think that completely replacing a tokio code is a good idea. Instead I think it can hidden behind a feature flag. So for tokio variant it will compile with tokio provided codec implementation and for async-std it may have a separate codec impl. |
Oh, I didn't mean to imply that we should replace tokio code across the board - just that we should strive to use runtime-agnostic crates when we can to be as flexible as possible. Most of the tokio-specific code seems to be in the But overall, I think the path forward is probably the following:
This should have a minimal impact on code that is heavily dependent on tokio, as most of that code is transport-specific and doesn't need to compile to WASM anyway |
I've implemented some first steps for this in #77 and #83 - it extracts most of the tokio specific code into one centralized location, and also refactors the rest of the code to use a runtime-agnostic codec type ( |
I've implemented the feature gating in #111 |
Would love to see this project succeed. https://stackoverflow.com/questions/52102612/is-it-possible-to-submit-http-requests-with-webassembly |
@mrdunky web assembly does not provide any networking apis in the spec, but because we are running in the browser we can call the browser's networking APIs via So to answer what I think you're asking, yes we can basically do anything we could do in javascript in a frontend context, including accessing network APIs |
One of Rust's strengths is is wasm support, letting developers run things natively in the web browser. It would be great if we could plan to support wasm in the future.
Looking into this, tokio currently doesn't support wasm, and async-std has a minimal feature set that supports wasm. It seems like a trivial way to use tcp in the browser is not yet feasible, but we should be on the lookout for ways to support it in the near future. One possible approach might be to use websockets instead of tokio sockets when compiling for wasm, and perhaps unbake the assumption that the sockets are managed by tokio by abstracting them with a common trait.
I also found that using websockets as a transport is an actual RFC, and seems to be the way things are going in the rest of the zmq libraries rather than trying to do tcp with websockets:
That implies that instead of trying to figure out how to get websockets to work with tcp, we can instead treat them as different transports using the
ws://
andwss://
notation. Maybe that would solve these issues?On the other hand, it looks like maybe tcp with websockets ought to still be supported. The javascript library I linked can do either
tcp://
orws://
.The text was updated successfully, but these errors were encountered: