Wasm Labs

Announcing Zig support for Wasm Workers Server

By Angel M Miguel
At 2023 / 09 10 mins reading

We are thrilled to announce that Wasm Workers Server (wws) now offers support for the Zig programming language, broadening the horizon for serverless application development. This exciting addition is made possible through a generous contribution from Liquid Reply, a cloud-native consultancy.

What is Zig?

Zig is a very young language, created only seven years ago. Despite its youth, it has started a thrilling and growing community around it.

It is a low-level systems programming language, aiming to be a safe alternative to the C programming language.

Zig has an interesting relationship with WebAssembly, too. It relies on LLVM for the codegen phase by default, and as a result, since its version 0.4.0 (April 2019), which did depend on LLVM 8, it has had support for creating wasm32-freestanding -- as it was named back then -- as well as WASI-based WebAssembly binaries. Its relationship with WebAssembly does not end there, though.

The Zig project did maintain a C++ implementation of the Zig compiler itself, leading to having to implement new language features twice (in the Zig compiler written in Zig, and in the C++ one). The reasoning behind this complexity and possible solutions is brilliantly described in a post by Andrew Kelley, Zig original author, and main maintainer. Thanks to WebAssembly and WASI they have been able to get rid of the C++ implementation of the Zig compiler. ๐ŸŽ‰

Regarding WebAssembly and its support as a target on Zig, although there is work ongoing for making Zig no longer depend on LLVM or clang libraries, it is still generating WebAssembly with the LLVM backend by default. Great progress is being made on their self hosted compiler -- considered experimental as of today --, with an 86% pass of behavior tests!

In summary, Zig is a very young language, with a growing and powerful community. It's a very good fit for performance critical applications, and for use cases close to the metal. Also, it has an amazing WebAssembly support, and take advantage of it on their own compiler development pipeline, signaling how important it is for them.

Your First Worker in Zig

To create your first worker in Zig, you need to install Zig on your machine. As Zig is still under heavy development, we recommend using version 0.11.0, as this is the version we have tested thoroughly.

After we ensured the availabililty of all prerequisites, we can start creating our worker. In this example, the worker will send a message "hello from zig".

  1. Create a new Zig project:

    $ zig init-exe
  2. Add Wasm Workers Server Zig SDK

    At this point in time Zig does not provide a package manager. We will therefore download the library to make it locally available.

    $ mkdir lib && \
    wget -O ./lib/worker.zig \
    https://raw.githubusercontent.com/vmware-labs/wasm-workers-server/main/kits/zig/worker/worker.zig
  3. Edit the src/main.zig file to match the following contents:

    const std = @import("std");\
    const worker = @import("worker");

    fn requestFn(resp: *worker.Response, r: *worker.Request) void {\
    _ = r;

    _ = &resp.headers.append("x-generated-by", "wasm-workers-server");\
    _ = &resp.writeAll("hello from zig");\
    }

    pub fn main() !void {\
    worker.ServeFunc(requestFn);\
    }
  4. Compile the project

    We recommend to use Zigs built-in build system to compile your code. For the sake of simplicity though, this is the minimum you need to compile the wasm binary:

    $ zig build-exe src/main.zig \
    --name worker \
    -mexec-model=reactor \
    -target wasm32-wasi \
    --mod worker::lib/worker.zig \
    --deps worker

    Note: if you rather want to use Zigs build system, please find some examples in our zig examples directory.

  5. Install Wasm Workers Server (if you don't have it yet):

    curl -fsSL https://workers.wasmlabs.dev/install | bash
  6. Run your worker

    $ wws .
    โš™๏ธ Loading routes from: .
    ๐Ÿ—บ Detected routes:
    - http://127.0.0.1:8080/worker => worker.wasm (name: default)
    ๐Ÿš€ Start serving requests at http://127.0.0.1:8080
  7. Finally, open http://127.0.0.1:8080/worker in your browser.

Congratulations! You just created your first worker in Zig ๐ŸŽ‰.

Learning Resources

For more information on developing with Zig on Wasm Workers Server, check out our comprehensive Zig documentation. You'll find examples and guides to help you with:

What's on the Horizon?

As always, we are committed to adding support for more programming languages to enhance the Wasm Workers Server ecosystem. In our roadmap, we have languages like PHP, Kotlin, and others queued for future releases.

Got a language you'd like to see supported in Wasm Workers Server? Don't hesitate to create an issue in our repository or reach out on Twitter.

A Word from Liquid Reply

Liquid Reply, the consultancy that contributed the Zig SDK, had this to say about their involvement:

"We are fascinated by the flexibility offered by Wasm Workers Server. It provides a unique platform to experiment with different programming paradigms in a serverless architecture, which is why we chose to contribute by adding Zig support to enable performance critical workloads." - Christoph Voigt, Liquid-Reply GmbH co-founder

Community Involvement

Your preferred programming language says a lot about you, but why limit yourself? With Wasm Workers Server, you can combine multiple languages in a single application. Thanks to the contributions from individuals or companies like Liquid Reply, we continue to add new languages to our repertoire.

Keep your eyes on our repository and Twitter for more exciting updates from the Wasm Workers Server community! ๐Ÿš€

Do you want to stay up to date with WebAssembly and our projects?