How to get TcpStream to work in WASM?


Hello! I have been trying for some time to open a TCP (or UDP, WebSocket,… ) connection from Rust compiled to Web Assembly without any luck. These are the two approaches I have tried:

  1. Use std: use the built-in implementations available in the standard library. Unfortunately, I always get OS error 115 (Operation in progress) when I do unwrap after TcpStream::connect. In the Firefox console, this appears as a failing WebSocket.
  2. Use websocket or some other crate for WebSockets: tried websocket, ws and some others, but I could not get any of these to compile.

Since there is not yet any good documentation on wasm in general, I have come here to you to look for the solution to this problem. Should I just use JavaScript and be happy with it? Is it my lack of knowledge of the front end? How does the standard library handle TCP/UDP in wasm, where it seems there is no true access to a pure transport layer connection?



It sounds like the TcpStream is running in a nonblocking mode. It looks like emscriptn/wasm support epoll, so mio and tokio might be ways to go there.


Thanks! I will give this a shot. Though I have taken a look at mio and it currently does not support emscripten. It does not complie at all. I will have to tinker with the source code to make it work, most probably… maybe add some cfg(target_os = "emscripten") here and there.

Well, at least that is what Github exists for…


Were u able to make it work? I was planning to write WASM node js module for Websocket, currently researching about if it is possible to run WS in WASM.