Wasmer JS SDK - WebAssembly In The Browser
Written by Nikos Vaggalis   
Wednesday, 07 February 2024

Introducing a new JavaScript library that makes running fully blown WebAssembly applications on the browser an easy task.

WebAssembly was touted as a runtime that would be embedded into every major web browser in order to write code in any language that could be compiled to its bytecode format. Using it, the popular browsers would be able to execute not just JavaScript but also compiled binaries, hence the second label that was attached to it - the JavaScript killer. .

As things progressed, both of those assumptions were debunked. WebAssembly has broken loose of the browser's constraints and can be used for running Microservices or serverless functions or even run Wasm binaries on the command line so that even cross-platform CLI applications can be developed. So, rather than kill JavaScript it might even interoperate with it. And the people at Wasmer know all about it having developed WASI(X).

But first of all let's get our terminology straight.

WASI (WebAssembly System Interface) is a modular system interface for WebAssembly (Wasm) that enables developers to run Wasm programs on any platform, including the browser, the cloud, and the edge. WASI is designed to be secure, fast, and portable, allowing developers to write their programs once and run them anywhere.

WASIX then is an extension to the WebAssembly System Interface (WASI) designed to make WebAssembly (Wasm) more compatible with POSIX programs, enabling the seamless execution of more complex applications in both the browser and server environments.

Surely they do sound a lot similar. What is the actual difference between them?

While WASI is flexible, the problem is that it doesn't yet fully support threads, Berkeley sockets, forking, and other POSIX features. WASIX bridges these gaps. It extends WASI with most of the missing POSIX features and is designed to run both in the server and the browser. As such it provides full support for efficient multithreading, sockets, current directory pthreads, process forking , subprocess spawning and waiting (exec, wait), TTY support, asynchronous polling of sockets and files, pipe and event support, etc.

Currently the only runtime that supports WASIX is Wasmer which, together with its JS SDK, makes running wasm applications on the browser and server a breeze. The added bonus is that the SDK, as already said, runs any UNIX program that can use threads, signals, subprocesses and other POSIX features all inside the browser!

A few examples of already ported such applications are:

  • FFMpeg
  • CPython (with threads and signals!)
  • Bash

While FFMpeg had been ported to JavaScript already, that was using Emscripten and WebAssembly, a combination that made the attempt not easy to perform. On the contrary, Wasmer and the SDK simplifies the process as you can see below:

wasmercode

Then Python. CPython too could already run in the browser in the form of Pyodide. Pyodide is the Python offspring of Iodide, the attempt to bring JavaScript and science to the browser. Iodide was already a success, but since JavaScript had no well-defined scientific stack, whereas Python does, the idea of replacing JavaScript with Python was suggested. And thus, Pyodide.

The solution was to make a Python interpreter run inside the browser's JavaScript virtual machine, loaded exclusively into the browser client-side without any remote components.
That of course happens because of Webassembly. The notion here is that your Perl, or Python, or whatever, code is not translated to JavaScript, but instead their respective interpreters/binaries are ported to WebAssembly, which subsequently targets the JavaScript VM living inside the browser.

All good, but Pyodide was again based on Emscripten and as such didn't support threading. WASIX changes that and makes Python available to run on both servers and browsers.

As for bash, just visit wasmer.sh and you'll be prompted with a shell!

Writing programs that use WASIX is very similar to writing normal programs in languages like Rust or C. The key difference is that you'll use the WASIX toolchain during compilation. WASIX currently supports Rust, C and Zig.

For instance to install the Wasix toolchain for Rust, run the following command:

$ cargo install cargo-wasix

which will install four things:

  • The cargo-wasix command-line tool
  • The wasix toolchain for rustup
  • The wasix target for rustup
  • The wasix target for rustc

As to how code a Wasm application in Rust, the official documentation has got you covered.
Then you can run the wasm binary from the CLI as:

$ cargo wasix run <path-to-wasm-file>

If want to run the binary in inside the browser then you can use the JS SDK like the FFMpeg example above.

In conclusion, thanks to Wasix you get enriched applications which can support everything POSIX entitles. That aside, the new SDK takes out the churn of porting your app in the browser or the server rendering WebAssembly the binary format of the future!

 

More Information

Wasmer

Wasmer Github

Related Articles

Running PostgreSQL Inside Your Browser

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Huawei Intends To Challenge iOS and Android
24/04/2024

Huawei has just changed its mind and decided to push its HarmonyOS to the rest of the world. A challenger to iOS and Android would be nice, but it is possible?



Amazon Ending Alexa Skills Payments
12/04/2024

Amazon has told developers who are signed up to the Alexa Developer Rewards Program that their monthly payments will end at the end of June. The announcement follows a decision to end the program unde [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Wednesday, 07 February 2024 )