Skip to content
Switch branches/tags
Go to file

Latest commit


Failed to load latest commit information.
Latest commit message
Commit time
Jan 31, 2021
Nov 27, 2019
Jan 21, 2021
Feb 18, 2021
May 28, 2020
Nov 27, 2019
Nov 27, 2019
Nov 26, 2019
Jan 28, 2021
Jan 22, 2021

Welcome to go-wasm-http-server 👋

Go Reference License: Apache 2.0 Twitter: njblepage

Embed your Go HTTP handlers in a ServiceWorker (using WebAssembly) and emulate an HTTP server!



Talk given at the Go devroom of FOSDEM 2021 explaining how go-wasm-http-server works:

Deploy a Go HTTP server in your browser Youtube link

The slides are available here.


go-wasm-http-server can help you put up a demonstration for a project without actually running a Go HTTP server.


go-wasm-http-server requires you to build your Go application to WebAssembly, so you need to make sure your code is compatible:

  • no C bindings
  • no System dependencies such as file system or network (database server for example)


Step 1: Build to js/wasm

In your Go code, replace http.ListenAndServe() (or net.Listen() + http.Serve()) by wasmhttp.Serve():

📄 server.go

// +build !js,!wasm

package main

import (

func main() {
    // Define handlers...

    http.ListenAndServe(":8080", nil)


📄 server_js_wasm.go

// +build js,wasm

package main

import (
    wasmhttp ""

func main() {
    // Define handlers...


You may want to use build tags as shown above (or file name suffixes) in order to be able to build both to WebAssembly and other targets.

Then build your WebAssembly binary:

GOOS=js GOARCH=wasm go build -o server.wasm .

Step 2: Create ServiceWorker file

Create a ServiceWorker file with the following code:

📄 sw.js



By default the server will deploy at the ServiceWorker's scope root, check registerWasmHTTPListener()'s API for more information.

You may want to add these additional event listeners in your ServiceWorker:

// Skip installed stage and jump to activating stage
addEventListener('install', (event) => {

// Start controlling clients as soon as the SW is activated
addEventListener('activate', event => {

Step 3: Register the ServiceWorker

In your web page(s), register the ServiceWorker:

  // By default the ServiceWorker's scope will be "server/"

Now your web page(s) may start fetching from the server:

// The server will receive a request for "/path/to/resource"
fetch('server/path/to/resource').then(res => {
  // use response...


For Go API see

JavaScript API

registerWasmHTTPListener(wasmUrl, options)

Instantiates and runs the WebAssembly module at wasmUrl, and registers a fetch listener forwarding requests to the WebAssembly module's server.

This function must be called only once in a ServiceWorker, if you want to register several servers you must use several ServiceWorkers.

The server will be "deployed" at the root of the ServiceWorker's scope by default, base may be used to deploy the server at a subpath of the scope.

See ServiceWorkerContainer.register() for more information about the scope of a ServiceWorker.


URL string of the WebAssembly module, example: "path/to/my-module.wasm".


An optional object containing:

  • base (string): Base path of the server, relative to the ServiceWorker's scope.
  • args (string[]): Arguments for the WebAssembly module.


👤 Nicolas Lepage

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2021 Nicolas Lepage.
This project is Apache 2.0 licensed.

This README was generated with ❤️ by readme-md-generator


Embed your Go HTTP handlers in a ServiceWorker and emulate an HTTP server!