Using asynchronous web APIs from WebAssembly

Using asynchronous web APIs from WebAssembly

The I/O APIs on the web are asynchronous, but they're synchronous in most system languages. When compiling code to WebAssembly, you need to bridge one kind of APIs to another. Using asynchronous web APIs from WebAssembly.

The I/O APIs on the web are asynchronous, but they're synchronous in most system languages. When compiling code to WebAssembly, you need to bridge one kind of APIs to another—and this bridge is Asyncify. In this post, you'll learn when and how to use Asyncify and how it works under the hood.

I/O in system languages [#]

I'll start with a simple example in C. Say, you want to read the user's name from a file, and greet them with a "Hello, (username)!" message:

#include <stdio.h>

int main() {
    FILE *stream = fopen("name.txt", "r");
    char name[20+1];
    size_t len = fread(&name, 1, 20, stream);
    name[len] = '\0';
    fclose(stream);
    printf("Hello, %s!\n", name);
    return 0;
}

While the example doesn't do much, it already demonstrates something you'll find in an application of any size: it reads some inputs from the external world, processes them internally and writes outputs back to the external world. All such interaction with the outside world happens via a few functions commonly called input-output functions, also shortened to I/O.

To read the name from C, you need at least two crucial I/O calls: fopen, to open the file, and fread to read data from it. Once you retrieve the data, you can use another I/O function printf to print the result to the console.

Those functions look quite simple at first glance and you don't have to think twice about the machinery involved to read or write data. However, depending on the environment, there can be quite a lot going on inside:

  • If the input file is located on a local drive, the application needs to perform a series of memory and disk accesses to locate the file, check permissions, open it for reading, and then read block by block until the requested number of bytes is retrieved. This can be pretty slow, depending on the speed of your disk and the requested size.
  • Or, the input file might be located on a mounted network location, in which case, the network stack will now be involved too, increasing the complexity, latency and number of potential retries for each operation.
  • Finally, even printf is not guaranteed to print things to the console and might be redirected to a file or a network location, in which case it would have to go via the same steps above.

webassembly

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

What is WebAssembly? WebAssembly Hello World

What is WebAssembly? WebAssembly Hello World - WebAssembly (wasm) is going to be the next big thing. WebAssembly hello world would show you how to get started with WebAssembly and write your first hello world program in it and execute it with JavaScript. WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine.

WebAssembly Tutorial: WebAssembly Debugging

In this WebAssembly tutorial: you'll see the current state of interactive debugging for WebAssembly and useful tips on how to do better. Debugging WebAssembly, as with any code, is critical for both developers and implementers.

What is WebAssembly? WebAssembly Hello World

WebAssembly (wasm) is going to be the next big thing. WebAssembly hello world would show you how to get started with WebAssembly and write your first hello w...

What is WebAssembly? Is WebAssembly Really the Death of JavaScript?

In this WebAssembly tutorial, you will look at what WebAssembly is, the future of JavaScript, Is WebAssembly Really the Death of JavaScript? Why WebAssembly matters and crucially what it means for JavaScript and the future of web development.

WebAssembly Tutorial: Build Your Own WebAssembly Compiler

This WebAssembly tutorial explains how to build your own WebAssembly compiler. Why do we need WebAssemby? Why create a WebAssembly compiler? Colin Eberhardt looks at some of the internals of WebAssembly, explores how it works “under the hood”, and looks at how to create a (simple) compiler that targets this runtime.