Practice your Go WebAssembly with a Game
Elevator Saga ported to Go WASM input
How it works
I’ve forked the original repository for the purposes of this exercise.
In order to accept Go WASM as input we’ll need to compile it server-side. I’ve built a small Go API service that sits behind an Nginx reverse proxy, takes the user input, creates a .go source file, compiles it to WASM in a new docker container via the Docker Go API and returns the output binary to the browser.
Compiling the Go WASM
I’ve written a boilerplate main.go file that provides the setup required for the game to run and makes use of the functions Init and Updated that are provided by the user:
The default/starter user input displayed in the game is the following:
The Init and Update functions above are the equivalents of the init and update functions required by the original game, as documented here https://didil.github.io/gowasm-elevatorsaga/documentation.html#docs
The API Server
The API/build server takes care of 2 important steps:
- Copying the main.go file and the input.go file (from the HTTP post request) to a temp folder.
- Running a Docker container which will perform the build, passing the previous temp folder as input. The Docker run is done via the Docker API and not via the shell, allowing for safer error handling.
The Dockerfile in itself is very simple and equivalent to running the following command in the directory containing our source files:
GOARCH=wasm GOOS=js go build -o app.wasm .
The actual JS/WASM integration is done in this JS file :
The JS code parses the server output, loads it into a WASM module and runs it.
We run the game by clicking on Apply and … it works !
The Nginx reverse proxy performs GZIP compression on the WASM output, decreasing the size from ~1MB down to ~300KB. I might revisit the results with TinyGo at some point as they promise much smaller binaries.
Server Side Caching
In the JS code above, you might have noticed the line:
let hash = SparkMD5.hash(json);
We hash the JSON input, which contains the input code and the compiler version (always go1.11.5 at the moment). We send that hash in the HTTP header Code-Hash. Nginx uses that header to cache WASM output. If you run an input that has already been compiled, the compiled WASM will return directly from the Nginx cache and the request will never touch the Go server, decreasing latency and saving resources. Client Side Caching could also be added in the future.
I hope you’ll have fun playing with Go WASM ! Here are a few resources to help you get started with the syntax: