How to call Rust functions from Node.js 🦀
Tags: rust, node, webassembly, webdev
Published: 2020-06-09 11:03:04

Create JavaScript + Rust hybrid applications in Node.js. 🦄 

The [Rust + Node.js hybrid apps](https://www.secondstate.io/articles/rust-functions-in-nodejs/) combine Rust's performance, WebAssembly's security and portability, and JavaScript's ease-of-use. A typical Rust + Node.js hybrid app works like this.

* The host application is a Node.js web application written in JavaScript. It makes WebAssembly function calls.
* The WebAssembly bytecode program is written in Rust. It runs inside [the open source Second State WebAssembly VM (SSVM)](https://www.secondstate.io/articles/ssvm-performance/
), and is called from the Node.js web application.

### Hello world for VSCode

We take this GitHub repo as an example.

{% github second-state/ssvm-nodejs-starter no-readme %}

* The Rust functions are in the src directory. You can put high-performance workload into Rust functions.
* The JavaScript functions are in the node directory and they can access the Rust functions.
* Use the node node/app.js command to run the application in Node.js.

The secret of no-software is VS codespaces. Code, build, and run directly from inside the browser. 

{% youtube j85cbNsciOs %}

> VS Codespaces runs entirely in your browser and costs around $1 per workday. It is cheaper than a cup of coffee in the office. Alternatively, use locally installed VSCode and Docker, and launch the IDE with your remote git repository.

1 First, open the [VS Codespaces](https://online.visualstudio.com/) web site and log in with your Azure account. You can get a [free Azure account](https://azure.microsoft.com/en-us/free/).

2 Next, create a new Codespace. Put your forked repository into the Git Repository field.

![Create a new Codespace](https://dev-to-uploads.s3.amazonaws.com/i/v6tsljah1hugxxw7ekb5.png)

3 Then open the `src/lib.rs`, `node/app.js`, and `Cargo.toml` files and see how the Node.js express app calls the Rust function to say hello.

![Code in Codespace](https://dev-to-uploads.s3.amazonaws.com/i/0d4snd66ufg5e9i542ro.png)

4 Click on the Run button on the left panel, and then the Launch Program at the top to build and run the application.

![Build and run](https://dev-to-uploads.s3.amazonaws.com/i/iwrxmf951s6djc2pyxbt.png)

The Terminal window at the bottom shows the build progress. It builds the Rust program and then launches the Node.js app.

![Build](https://dev-to-uploads.s3.amazonaws.com/i/gyh93y76h3uygnqxstn8.png)

The Debug window shows the Node.js server running and waiting for web requests.

![Debug](https://dev-to-uploads.s3.amazonaws.com/i/52br72mrx2cw1qadfomc.png)

5 Now, you have two choices. You could use the proxy link for `127.0.0.1:3000` to access the running server in a browser.

![Browser link](https://dev-to-uploads.s3.amazonaws.com/i/nd21rrbeldjp9yri69od.png)

Or, you could open another terminal window in the IDE via the `Terminal -> New Terminal` menu.

![Open Terminal](https://dev-to-uploads.s3.amazonaws.com/i/dk22juyemyve6l87q84n.png)

From the terminal window, you can test the local server.

```
$ curl http://127.0.0.1:3000/?name=SSVM
hello SSVM
```

### More exercises

Now, you can copy and paste code from [this project](https://github.com/second-state/wasm-learning/tree/master/nodejs/functions).

* `src/lib.rs` --> Replace with [code here](https://github.com/second-state/wasm-learning/blob/master/nodejs/functions/src/lib.rs)
* `Cargo.toml` --> Replace with [code here](https://github.com/second-state/wasm-learning/blob/master/nodejs/functions/Cargo.toml)
* `node/app.js` --> Replace with [code here](https://github.com/second-state/wasm-learning/blob/master/nodejs/functions/node/app.js)

Click on Run to see the build output in the Terminal window, and application console output in Debug window.

Try to log into GitHub from the IDE, and use the IDE's GitHub integration features to commit the changes, push the changes back into your forked repository, and perhaps even send us a Pull Request from the IDE!

### Resources

* [The Second State VM (SSVM)](https://github.com/second-state/ssvm) is a high-performance WebAssembly virtual machine designed for server-side applications.
* [The SSVM NPM addon](https://github.com/second-state/ssvm-napi) provides access to the SSVM, and programs in it, through a Node.js host application.
* [The SSVM ready tool, ssvmup](https://github.com/second-state/ssvmup) is a toolchain for compiling Rust programs into WebAssembly, and then make them accessible from JavaScripts via the SSVM.

Stay in touch! Get the [email newsletter](https://webassemblytoday.substack.com/?ref=dev.to) on Rust, WebAssembly, serverless, blockchain, and AI.