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
Wishlist: WebSocket #23
Comments
I did put together a working implementation; it'll need some polishing, but at least demos the most basic functionality. |
Thanks for the invitation @tcr, but I'm both rather new to Rust and to emscripten, so I wouldn't be very helpful in maintaining this library. (Have a look at the code to see why; this is about the first non-hello-world-ish code I've written in Rust). My websocket branch now contains something usable for transferring binary data, but
I've published a usage example at https://github.com/chrysn/wasm-websocket-demo . |
It would be nice to bypass JavaScript and the whole WebSocket DOM event registration and message event data extraction completely, but that would need to be supported by WebAssembly and AFAICT isn't right now. There is a mechanism in emscripten around |
Not to derail any effort here, but I've also been working on emscripten socket support lately. You may find this pull request useful. In the comments is a link to a repo with wasm and asmjs examples. |
@tones111, i'd be happy to see my branch superseded by something different, but it seems to me that your mio integration adds emscripten's socket support (which is what I commented on in #23 (comment): providing something that looks like a TCP socket implemented using WebSocket). What would be a great combination, though, is if my branch (or anything else providing the browser's WebSocket to Rust) could be made into a standard mio WebSocket client (maybe https://ws-rs.org/?), so that developers don't need to use different WebSocket APIs on different implementations. |
My appologies if the mio integration doesn't align with what you're looking for, but I'm failing to understand why it wouldn't. The original post for this issue asks for the ability to...
which is what I believe I've been able to accomplish via the Emscripten socket API. Emscripten sockets need a websocket server on the other end to communicate with. If you look at the traffic in wireshark you can see the HTTP negotiation and protocol upgrade. As I understand it Emscripten's sockets only operate with a "binary" protocol. Going this route skips having to implement anything in javascript (handled by Emscripten) and allows the client-side rust to focus on the data going over the socket. If you want a concrete example my repo at https://github.com/tones111/mio-example builds up a server application using HTML (hyper) and websocket (ws-rs) and serves wasm to the browser. That wasm code is using a mio socket to talk to the port opened by ws-rs. As far as I know Emscripten is currently the only way to get rust running in the browser. I imagine there will be other socket APIs as wasm or LLVM support matures, but you can't target something that doesn't exist yet. Again, apologies if I'm misinterpreting the request. |
The Emscripten API you're using is sending binary frames, but it's expressing that process as a TCP socket. That means that no subprotocols are exposed, only binary frames can be exchanged and, (worst IMO) that emscripten would be free to join messages and send or deliver them as a merged (although they don't right now). That works fine if what one wants to substitute a TCP connection by terminating it at the server and tunneling it through a websocket to the browser, but not for using a WebSocket's potential. With respoect to emscripten being required, AFAICT emscripten primarily provides an operating-system-like environment to programs running inside. That's nice to get |
Thanks. I agree with you're points on emscripten's limitations. I'm hopeful that wasm will define apis that map more closely to the underlying functionality. Good luck with your implementation. |
hey! this issue and this repo1 show up when searching for "rust wasm websocket". Are there any pointers for somebody looking for wasm websockets in 2021? :) Thanks! |
Please consider exposing the browser's WebSocket API in rust-webplatform.
I did not yet try implementing this myself (still being busy on the other end of the socket), but if from Rust I could establish WS connections and send/receive binary data frames (probably as a JavaScript ArrayBuffer), that should allow smooth integration of the existing coap crate for CoAP over WebSockets communcation.
The text was updated successfully, but these errors were encountered: