Hello,
I've been researching about wasm/rust interop, I want to use it on some real-life situation, so I was thinking in make a WebRTC and/or WebSockets servers using wasm, does this makes any sense at all? Regards
In order to access WebSockets inside of WebAssembly, you would have to run your code on Node.js, and you also need to create some shims (using the wasm-bindgen attribute).
That does work, though it will probably be a lot easier (and faster) to just write an ordinary Rust server (without WebAssembly). There are a few WebSockets libraries that you can use for that purpose.
On the other hand, creating a Rust+WebAssembly+WebSocket client makes a lot of sense, since that allows you to use the same language (Rust) on both the client and server.
Nice :), well now having in mind that it would make sense on the client side, what would be the best future-proof approach now that Host Bindings is coming up next? would I have to rewrite a lot of code if I do it via wasm-bindgen? or would it be stdweb a better solution? thx
Haven鈥檛 done this yet (plan to soon actually) so forgive any slight mistakes here but:
The rust wasm book has an example of accessing a vector of binary data from JS
https://rustwasm.github.io/book/game-of-life/implementing.html
So you could probably store your web socket message as a Vec\
Better yet if you can send over a u8 array and not re-allocate ever (docs should say if u8 arrays are supported for sending to JS but I鈥檇 assume so).
But tl:dr pass your binary data to JS and then send the binary data via a WebSocket that you鈥檝e created on the JS side.
Could also send over a string if you need to.
Sorry for the brevity / lack of links to some of this stuff.. on mobile and trying to quickly answer.. hope it helps!!!
@chinedufn thx for the insights, so it seems this is feasible, I'll try to keep digging more 馃憤
Nowadays the web_sys crate should have all bindings for WebRTC. To that end all the pieces are in theory there, but there aren't currently any examples of doing this. One would always be appreciated here though!
I'm gona close this for now as all the pieces should be there on the wasm-bindgen side of things
Most helpful comment
Nowadays the
web_syscrate should have all bindings for WebRTC. To that end all the pieces are in theory there, but there aren't currently any examples of doing this. One would always be appreciated here though!I'm gona close this for now as all the pieces should be there on the
wasm-bindgenside of things