webassemblyemscripten

What is webassembly for socket-based applications, http or non-http( eg. MQTT) web based protocol


I have started learning about WebAssembly few days back. I understand that WebAssembly is a compiled binary which can be used along side javascript to run applications written in languages like C/C++, Rust etc with minimal changes.

My question is , if I am implementing a TCP/IP (or UDP) Socket program or a HTTP based server and use emscripten SDK to compile it and run in browser what exactly I am getting ? What are the benefits ? Does this mean, that I was able to run server from a terminal earlier and use browser or postman as client to do GET/POST requests. And currently with WebAssembly I am able to run that application over browser( which effectively means see the console over browser) ?

How does WebAssembly help if I can convert a IoT protocol like MQTT to WebAssembly ? What can be the use case for doing it ? How is it different from implementing a MQTT-HTTP proxy to talk to edge cloud ?

Also is the WebAssembly binary running on client side ? If yes, users will be using browsers' local runtime to run the application after downloading the webassembly binary on client sides ?


Solution

  • WebAssembly (WASM) in the browser is executed on a user's local browser and is limited to the capabilities and restrictions of the browser. Since at this time browsers do not allow for socket-based networking, any program you compile to WASM cannot do things like open sockets or directly communicate via non-web networking protocols like MQTT.

    Emscripten as a library tries to emulate POSIX networking functionality by proxying over WebSockets, XHR or fetch. It puts restrictions on how you build your server and likely many existing applications will need to be modified in order to work with Emscripten. You'll also need an additional proxy server hosted somewhere else, like a web server or local machine. So with Emscripten it is theoretically feasible to talk to a web server from the browser or host a server in the browser, but with the specific caveats I mentioned.

    WASM also can be run outside the browser, such as part of a web server or embedded system. In each of those different environments there may be less or different restrictions. WASI is the standardization effort for bringing APIs to WASM in a secure and portable way, such as sockets and HTTP.