Creating Your First WebAssembly App: A Browser-Only Guide Using Emscripten and GitHub Codespaces

By ✦ min read

Welcome to the world of WebAssembly! This guide shows you how to write, test, and deploy your first WebAssembly program—all without installing anything on your local machine. By combining Emscripten, a powerful compiler toolchain, with GitHub Codespaces (a cloud-based development environment), you can compile C code into WebAssembly and build a fully functional web app directly in your browser. Below, we answer common questions to get you started quickly and efficiently.

What is WebAssembly and why use it for web apps?

WebAssembly (Wasm) is a low-level binary instruction format that runs in modern web browsers at near-native speed. It allows developers to compile code from languages like C, C++, or Rust into a compact, efficient module that can be executed alongside JavaScript. Using WebAssembly is beneficial because it unlocks performance-critical tasks (e.g., video editing, game engines, data processing) directly on the web, without plugins. With Emscripten, you can port existing C libraries or write new code and integrate them seamlessly into web applications. This approach combines the portability of JavaScript with the speed of compiled languages, making it ideal for computationally intensive features.

Creating Your First WebAssembly App: A Browser-Only Guide Using Emscripten and GitHub Codespaces
Source: towardsdatascience.com

Do I need any local installation to create a WebAssembly program?

No local installation is required. This entire workflow is designed to run inside a web browser using GitHub Codespaces, a cloud-hosted development environment. Codespaces provides a full Linux virtual machine with preinstalled compilers (including Emscripten) and tools. You create a repository on GitHub, open it in Codespaces, and write your C code directly in the browser’s editor. The compilation and testing also happen remotely. Once finished, you can deploy the app using GitHub Pages or other hosting services—still from within the same browser session. This eliminates the need to set up compilers, configure environment variables, or install dependencies on your own computer, making it perfect for beginners or quick experiments.

How do I compile C code into WebAssembly using GitHub Codespaces?

First, create a GitHub repository and open it with Codespaces (click the green "Code" button and choose "Open with Codespaces"). Once the environment loads, you’ll see a terminal at the bottom. Install Emscripten by running git clone https://github.com/emscripten-core/emsdk.git and then cd emsdk && ./emsdk install latest && ./emsdk activate latest && source ./emsdk_env.sh. Write your C program (e.g., hello.c) with a function like EMSCRIPTEN_KEEPALIVE int add(int a, int b) { return a + b; }. Then compile using emcc hello.c -o hello.html -s EXPORTED_FUNCTIONS="['_add']" -s EXPORTED_RUNTIME_METHODS="['ccall']". This produces hello.html, hello.js, and hello.wasm. The HTML file includes a simple UI to call your C function from JavaScript.

What steps are involved in writing a simple C program and converting it?

Start by creating a new file named calc.c in your Codespaces workspace. Write a small C program that exposes a function to add two numbers. For example:
#include
EMSCRIPTEN_KEEPALIVE int add(int a, int b) { return a + b; }

Next, open the terminal and compile the file with emcc calc.c -o calc.html -s EXPORTED_FUNCTIONS="['_add']" -s EXPORTED_RUNTIME_METHODS="['ccall']". Emscripten generates three files: calc.html (a ready-to-use page), calc.js (the JavaScript glue code), and calc.wasm (the compiled binary). The HTML page automatically imports the module and allows you to call _add from a button. You can modify the HTML to add input fields and a display result. For advanced usage, the generated calc.js provides Module.ccall() to invoke C functions directly.

Creating Your First WebAssembly App: A Browser-Only Guide Using Emscripten and GitHub Codespaces
Source: towardsdatascience.com

How do I integrate the compiled WebAssembly module into an HTML page?

When you compile with emcc and specify an output HTML file (like calc.html), Emscripten automatically creates a fully functional page that loads the Wasm module and exposes C functions to JavaScript. If you prefer to embed the module into an existing page, compile to JavaScript only: emcc calc.c -o calc.js -s EXPORTED_FUNCTIONS="['_add']" -s EXPORTED_RUNTIME_METHODS="['ccall']". Then in your own HTML, include the calc.js script. After the module loads, you can call Module.ccall('add', 'number', ['number', 'number'], [3, 4]) to get the result (7). The JavaScript glue code handles fetching and instantiating the .wasm file. Ensure the .wasm file is in the same directory or adjust the path using the locateFile option in Module.

How do I deploy my WebAssembly web app from GitHub Codespaces?

After creating your WebAssembly app, you can deploy it directly from Codespaces using GitHub Pages. First, commit and push your files (include all generated .html, .js, and .wasm files) to your GitHub repository. In the repository settings, enable GitHub Pages from the main branch (or a gh-pages branch). Your app will be published at https://.github.io//. Alternatively, you can use a static hosting service like Netlify or Vercel—simply drag and drop the project folder. Since Codespaces already has Git configured, you can also run npm install -g surge and deploy with surge ./your-folder from the terminal. This browser-only workflow means your entire development cycle (write, test, compile, deploy) happens without leaving the browser.

Tags:

Recommended

Discover More

Building the Future: How the Genesis Mission Merges AI and Energy LeadershipRevolutionizing Violin Design: MIT's Physics-Based Virtual InstrumentMassive Bering Strait Dam Proposed to Halt Catastrophic Ocean Current Collapse10 Essential Steps to Build a Natural Language Interface for Spotify Ads with Claude Code Plugins10 Ways Amazon WorkSpaces Empowers AI Agents to Supercharge Your Enterprise Workflows