🍹 High Performance Node.js on the $5 Computer - Raspberry Pi

Jul 08, 2020 • 5 minutes to read

The Raspberry Pi is a very powerful computer in a tiny package. The cheapest option, the Raspberry Pi Zero, is capable of running a fully featured Linux distribution and driving a high definition display. It is the size of 3 coins (US Quarters) and costs $5. At $10, the Raspberry Pi Zero W comes with integrated WiFi and Bluetooth.

Get a FREE Raspberry Pi kit ($25 value)

raspberry pi

With a generic ARM CPU and easy networking, the Raspberry Pi can easily become a personal application server for you. For example, you can put a web application (eg a collaborative record keeping app) on a Pi, bring it to a meeting, and make it accessible to everyone in the room. You do not even need the Internet. It is completely decentralized and censorship resistant.

The personal server is especially useful for developers. You can have a separate environment to deploy and test your server-side applications without having to mess with your laptop. The personal dev server is like Docker on steroids.

However, the $5 Pi is also, obviously, a resource constrained server. It only has 512MB of RAM and a single CPU core. It could benefit greatly from a lightweight and high-performance application runtime. But at the same time, we still like the ease-of-use and developer productivity of “heavy weight” scripting languages such as JavaScript. We want best of both worlds.

Our solution is to deploy high-performance and resource-efficient Rust functions inside Node.js JavaScript apps. The Second State WebAssembly VM (SSVM) provides a light, efficient, secure, and portable runtime for Rust code. In this article, I will teach you how to set it up.

Setup Ubuntu on Raspberry Pi

To use the Raspberry Pi as a personal dev server, I highly recommend installing the latest Ubuntu Server 20.04 TLS. Just use the Raspberry Pi Imager to select and load the Ubuntu Server 20.04 image onto a MicroSD card. Remember to set up a WiFi connection on the MicroSD card or after you login.

If you buy a Raspberry Pi kit with a preloaded MicroSD card, it probably comes with the Raspberry Pi OS (called NOOBS). It works with SSVM version 0.3.6 and ssvmup version 0.1.11. You can make it work, but it requires some effort.

Put the MicroSD card into your Pi device’s card slot, connect an HDMI display, keyboard, mouse, and power up! Follow the on-screen instructions to install Ubuntu Server 20.04. From there, you can create a user account, connect to WiFi, turn on SSH, and open the command line terminal. In order to use the Pi device as a “headless” server, you could request a static IP address from your router. In the future, you can just power it on, and connect to it via SSH from your laptop — there is no need for display, keyboard, and mouse. Once you are set up, use the following command to find your Pi’s IP address on your local network.

$ hostname -I
192.168.2.108 172.17.0.1

As with all new Linux installations, it is a good idea to update and upgrade to the latest packages. Run the command below and be patient. It could take an hour.

$ sudo apt update && sudo apt upgrade

Next, run the following command to install essential developer libraries.

$ sudo apt install build-essential curl unzip libboost-all-dev

Install Node.js and SSVM

The following two commands to install Node.js on your Pi. Refer to this article to learn how to install Node.js above v11.15.0.

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
$ sudo apt install nodejs

From here, you can use npm to install modules. Here we install the Second State VM (ssvm) to support high performance Rust functions in Node.js applications.

$ npm install ssvm

Next, let’s try to run a couple of demo applications.

Run a demo Node.js application

Get the demo application from the Internet, and unzip the compressed archive.

$ curl -O https://www.secondstate.io/download/quadratic.zip
$ unzip quadratic.zip

Next, run a test program to make sure that the Node.js JavaScript function can correctly call the Rust function through SSVM.

$ cd quadratic/node
$ node test.js
[0.5,-3.0]

Start the Node.js server application from the command line terminal.

$ npm install express // Required for the web app.
$ cd quadratic/node
$ node server.js

Point the browser to http://localhost:8080/ or you can access it from another computer on your network.

raspberry pi

It is a web application that teaches math and solves quadratic equations. It could come really handy in a small group in a classroom!

Install developer tools

You do not really need developer tools on a personal server. But the Raspberry Pi device is powerful enough to compile and build software. In fact, one of its common uses cases is to teach programming. The Raspberry Pi OS comes pre-loaded with developer tools for Java, Python, and Scratch. Now, let’s install some serious tools on it! I always install Git on all my development environments.

$ sudo apt install git

The following command installs the Rust compiler toolchain on the Pi.

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

Run the following command to set up the correct path without logging out and back in again.

$ source $HOME/.cargo/env

Next, you can clone our Rust learning repository, and learn from examples.

$ git clone https://github.com/second-state/wasm-learning.git

Here is the hello world example. Have fun!

$ cd wasm-learning/rust/hello
$ cargo build
   Compiling hello v0.1.0 (/home/pi/Dev/wasm-learning/rust/hello)
    Finished dev [unoptimized + debuginfo] target(s) in 4.35s
$ target/debug/hello
Hello, world!

Check out the official Rust web site and the Rust by Example books for more learning resources!

Next steps

Now you have everything you need on the Raspberry Pi device. The next steps are to learn more about creating high-performance and resource-constrained Node.js web applications on your $5 Raspberry Pi personal dev server.

Have fun, and let us know how you used your Raspberry Pi device!

RustRaspberry PiNode.jsWebAssemblygetting-started
A high-performance, extensible, and hardware optimized WebAssembly Virtual Machine for automotive, cloud, AI, and blockchain applications