DEV Community

Alex Spinov
Alex Spinov

Posted on

Wasmer Has a Free API: Run WebAssembly Packages Like Docker Containers

Docker images are 100MB+. WebAssembly packages are <1MB. Wasmer lets you run them like containers — with a package registry and everything.

What Is Wasmer?

Wasmer is a WebAssembly runtime that can run Wasm modules from any language. Plus, it has a package registry (WAPM/wasmer.io) like npm for Wasm.

# Install
curl https://get.wasmer.io -sSfL | sh

# Run a Wasm package
wasmer run python/python -- -c "print('Hello from Wasm!')"
wasmer run syrusakbary/cowsay "Hello WebAssembly!"
wasmer run php/php -- -r "echo 'Hello from PHP in Wasm!';"
Enter fullscreen mode Exit fullscreen mode

Python, PHP, Ruby, Lua — running as WebAssembly. No native install needed.

Use as a Library

use wasmer::{Store, Module, Instance, Value};

fn main() -> anyhow::Result<()> {
    let mut store = Store::default();
    let module = Module::from_file(&store, "add.wasm")?;
    let instance = Instance::new(&mut store, &module, &imports! {})?;

    let add = instance.exports.get_function("add")?;
    let result = add.call(&mut store, &[Value::I32(5), Value::I32(3)])?;
    println!("5 + 3 = {}", result[0].unwrap_i32());  // 8
    Ok(())
}
Enter fullscreen mode Exit fullscreen mode
from wasmer import engine, Store, Module, Instance

store = Store(engine.JIT())
module = Module(store, open('add.wasm', 'rb').read())
instance = Instance(module)
result = instance.exports.add(5, 3)
print(f"5 + 3 = {result}")  # 8
Enter fullscreen mode Exit fullscreen mode

Wasmer Edge (Serverless)

# Deploy a Wasm app to the edge
wasmer deploy
# Running globally in <1ms cold start
Enter fullscreen mode Exit fullscreen mode

Why Wasmer

  • Universal — run Wasm from Rust, Python, Go, JavaScript, C, Ruby, PHP, Java
  • Package registrywasmer run author/package like npx
  • Near-native speed — Cranelift/LLVM JIT compilation
  • Sandboxed — Wasm modules can't access anything not explicitly allowed
  • Tiny — packages are kilobytes, not megabytes
  • Edge deploy — sub-millisecond cold starts

Building Wasm applications? Check out my developer tools or email spinov001@gmail.com.

Top comments (0)