A template project to run Rust functions in Node.js through the Second State WebAssembly engine.
Info
This project is no longer updated. If you are interested in using javascript, please refer to the wasmedge-quickjs repo
Getting started
Fork this project to create your own Rust functions as a web service.
How it works
- The Rust function source code is in the
src/lib.rsfile. - Use the rustwasmc tool to compile the Rust function into a WebAssembly module in the
pkgdirectory. - Optional: Use the
node node/app.jscommand to test the function locally in Node.js.
pkg/.wasm file to the Second State FaaS service, OR to a Node.js server, to turn it into a web service.
Setup
It is easy to use our appdev Docker image to run the dev environment. You can choose from x86_64 or aarch64 versions of the image. Alternatively, you could also use Github Codespaces to run the following example.
$ docker pull wasmedge/appdevx8664
$ docker run -p 3000:3000 --rm -it -v $(pwd):/app wasmedge/appdevx8664
(docker) #
Build
Use the rustwasmc command to build the Rust function into a WebAssembly bytecode file.
(docker) # cd /app
(docker) # rustwasmc build
Test and debug
From the first terminal window, start the Node.js application.
(docker) # node node/app.js
From a second terminal window, you can test the local server.
$ curl http://localhost:3000/?name=WasmEdge
hello WasmEdge
Optional: Upload to the FaaS and test
Upload the wasm file in the pkg folder to the FaaS. Double check the .wasm file name before you upload.
(docker) # curl --location --request POST 'https://rpc.ssvm.secondstate.io:8081/api/executables' \
--header 'Content-Type: application/octet-stream' \
--header 'SSVM-Description: say hello' \
--data-binary '@pkg/hellolibbg.wasm'
The FaaS returns
{"wasmid":161,"wasmsha256":"0xfb413547a8aba56d0349603a7989e269f3846245e51804932b3e02bc0be4b665","usagekey":"00000000-0000-0000-0000-000000000000","adminkey":"00xxxxxx-xxxx-xxxx-xxxx-4adc960fd2b8"}
Make a function call via the web.
(docker) # curl --location --request POST 'https://rpc.ssvm.secondstate.io:8081/api/run/161/say' \
--header 'Content-Type: text/plain' \
--data-raw 'Second State FaaS'
hello Second State FaaS
You can easily incorporate this web service into your HTML web pages. See how
More exercises
Now, you can copy and paste code from this project.
src/lib.rs--> Replace with code hereCargo.toml--> Replace with code herenode/app.js--> Replace with code here
- FaaS deployment: https://github.com/second-state/wasm-learning/tree/master/faas
- Node.js deployment: https://github.com/second-state/wasm-learning/tree/master/nodejs
Read more:
* Getting started with FaaS in Rust * Use Binary Data as Function Input and Output * Mixing Text and Binary Data in Call Arguments * Internet of Functions: webhooks * AI as A Service on WebAssembly * The Case for WebAssembly on the Server-side * Getting started on Rust and WebAssembly for server-side apps * Passing function arguments in JSON * Use Tensorflow and AI models from WebAssemblyResources
- The WasmEdge Runtime is a high performance WebAssembly virtual machine designed for edge computing (including Edge Cloud) applications.
- The rustwasmc is a toolchain for compiling Rust programs into WebAssembly, and then make them accessible from JavaScripts via the WasmEdge Runtime.
- The Second State FaaS is an open source FaaS engine based on WebAssembly and Node.js.