1 of 20
2 of 20
3 of 20
4 of 20
5 of 20
6 of 20
7 of 20
8 of 20
9 of 20
10 of 20
11 of 20
12 of 20
13 of 20
14 of 20
15 of 20
16 of 20
17 of 20
18 of 20
19 of 20
20 of 20

site

doc

blog

success stories

Creating a Function using WebAssembly on Azion’s platform

About WebAssembly

WebAssembly (abbreviated Wasm) allows you to develop high-performance web applications using open web platform technologies and different languages. It can be thought of as an alternative to JavaScript, but not a replacement for it. It’s intended to run next to it. This way, developers can use both languages however they prefer, as the best of both worlds since they can run together.

Although WebAssembly is a low-level language with a tight binary format, allowing it to deliver near-native performance for applications, it is not designed to be used as a language itself, but as an effective compilation target for languages like C, C++,Rust and others.

It was created as an open standard inside the W3C WebAssembly Community Group with the following goals:

  • Be fast, efficient, and portable;
  • Be readable and debuggable;
  • Keep secure;
  • Keep secure;

Step-by-step: creating an Edge Function using Wasm

Feching code

  1. Log into the Real-Time Manager;
  2. On the Edge Libraries menu, click on Edge Functions;
  3. Click on Add Function to add a new Edge Function;
  4. Create a name for your function;
  5. Select JavaScript.

Syntax

//
// DESCRIPTION: fetching the WASM function binary with a loop and responding summary (value on parameter '?loop=<value>')
//
{
    async function handleRequest(request) {
            let wasmResponse = await fetch("http://domain/loop.wasm");
            let wasmArray = await wasmResponse.arrayBuffer();
            let module = await WebAssembly.compile(wasmArray);
            let instance = new WebAssembly.Instance(module);
            const loop = instance.exports.loop;
            let count = 1
            let params = (new URL(request.url)).searchParams;
            if (params.has("loop")) {
                let number = params.get("loop");
                if (!isNaN(number)) {
                    count = parseInt(number);
                }
            }
            let value = loop(count);
            return new Response("loop sum = " + value + "\n");
    }

    addEventListener("fetch", (event) => {
        event.respondWith(handleRequest(event.request));
    });
}

Didn’t find what you were looking for? Open a support ticket.