🛠 Set up Rust & WebAssembly in Node.js

• 3 minutes to read

There are great use cases for WebAssembly on the server-side, especially for AI, blockchain, and big data applications. You can write functions in Rust, compile into WebAssembly, and call the managed code in Node.js applications on the server. This approach combines Rust's performance, WebAssembly's security and portability, and JavaScript's ease-of-use.

Docker setup

The easiest way to set up your dev environment is to use Docker. We provide a Dockerfile for just this purpose. First, clone the starter template project.

$ git clone https://github.com/second-state/ssvm-nodejs-starter
$ cd ssvm-nodejs-starter

Pull a Docker image with the prerequisite software stack.

$ docker pull secondstate/ssvm-nodejs-starter:v2

Next run the Docker image, and mount your local workspace. This way, you can run software inside the Docker container against your local files. You can also map ports from Docker to the local machine so that you can test web applications.

$ docker run -p 3000:3000 --rm -it -v $(pwd):/app secondstate/ssvm-nodejs-starter:v2
(docker) $ cd /app

That's it. You can now build and deploy software from the (docker) # command line. All changes to your code will be saved in your local files regardless whether you stop or delete the Docker container.

Manual setup

Of course, not everyone wants to use Docker. In the rest of the article, we will walk through setps for a manual setup in a fresh Ubuntu Server 20.04 distribution. The whole system takes 10 minutes to set up. We will install Rust, Node.js, the Second State WebAssembly VM (SSVM), and the ssvmup compiler tool.

Setup the Operating System

We recommend you use the Ubuntu 20.04 LTS or later. The SSVM depends on the latest version of the libstdc++ library, which is already installed on Ubuntu 20.04. If you are using another Linux distribution, please make sure that GLIBCXX >= 3.4.28 or upgrade as needed.

Then, use the commands below to bring your operating system up to date with the latest developer tools.

$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo apt install -y build-essential libboost-all-dev llvm-dev liblld-10-dev
$ sudo apt install -y curl wget git vim pkg-config libssl-dev // Optional dev tools

If you are using the Tensorflow WASI API for AI-as-a-Service applications. Install the Tensorflow library too.

$ wget https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.3.0.tar.gz
$ sudo tar -C /usr/local -xzf libtensorflow-cpu-linux-x86_64-2.3.0.tar.gz
$ sudo ldconfig

Rust

Installing Rust on your Linux machine is very easy. Just two commands.

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env

For more options on installing Rust on different systems, please refer to the official Rust documentation.

Node.js

The best way to install Node.js on Linux is through the NVM tool. NVM allows you to use the latest Node.js, and change easily when you need to test your code on a different version of Node.js.

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
$ export NVM_DIR="$HOME/.nvm"
$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
$ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

$ nvm install v12.18.3
$ nvm use v12.18.3

For more options on installing Node.js on different systems, please refer to the official Node.js documentation.

Second State VM

The Second State Virtual Machine (SSVM) is an open source WebAssembly runtime optimized for server-side applications. Use the following commands to install it as a Node.js module.

$ npm install ssvm

The ssvmup compiler toolchain

The ssvmup tool provides an easy command to compile Rust functions into WebAssembly and it automatically generates Node.js and SSVM integration code. Since you already have Node.js installed, this just requires one command.

$ npm install -g ssvmup # Append --unsafe-perm if permission denied

What’s next?

Now you have all the tools, get started with a hello world example!

RustWebAssemblyNode.jsgetting-startedrust-function-in-nodejs
Fast, safe, portable and serverless Rust functions as services