Skip to content

Web

A Bellui app can run in a web browser. Go compiles it to WebAssembly (a binary format browsers can run), and the app draws into a canvas element on the page. Rendering uses the browser’s WebGPU by default, or WebGL with the gl build tag.

The quickest way: bellui serve

In your app’s folder, run:

bellui serve

This builds the web version, writes it to a wasm folder, and serves that folder at http://localhost:8080. Open the address in a browser that supports WebGPU. --http-port picks another port, and --debug builds a debug version; by default serve builds in release mode. serve stops on Ctrl-C or when it is sent SIGTERM.

Opening the app on a phone

Browsers offer WebGPU only to a secure page: one served over https, or from localhost. A phone that opens http://<your computer's address>:8080 gets neither, so the page says “WebGPU needs a secure page: open this app over https, or at localhost on this computer.” Two flags give the phone a secure page:

bellui serve --https     # a self-signed certificate, on your network
bellui serve --tunnel    # a public https address through Cloudflare
  • --https serves over https with a self-signed certificate made for this computer’s addresses. serve prints each network address to open and the certificate’s SHA-256 fingerprint. The browser warns about the certificate once per run before it opens the page. Compare the fingerprint before you accept it.
  • --tunnel also opens a Cloudflare quick tunnel and prints its public trycloudflare.com address. Browsers trust that address’s certificate, so there is no warning. It needs Cloudflare’s cloudflared command on your PATH, and no Cloudflare account. Anyone who has the address can open the app while serve runs. The tunnel closes when serve stops.

Packaging for a web server

bellui package -os web

The wasm folder it creates is a complete static site:

FilePurpose
index.htmlThe page. It shows your icon and a loading message, checks that the browser supports WebGPU (or WebGL, for a build with -tags gl) and says so if it does not, loads the app, and hides the loading screen when the app creates its canvas. It also credits the gopher artwork on the loading screen, which its licence requires.
<Name>.wasmYour compiled app.
wasm_exec.jsGo’s WebAssembly loader, copied from your Go installation.
icon.png, light.css, dark.css, spinnersPage icon and loading-screen styling, in the user’s light or dark preference.
bellui-sw.jsA service worker used for notification action buttons.

Copy the folder to any static web host. Add --release for a smaller, optimised build.

Building it yourself

You can also build with the Go tool and write your own page:

GOOS=js GOARCH=wasm CGO_ENABLED=0 go build -o app.wasm .              # WebGPU
GOOS=js GOARCH=wasm CGO_ENABLED=0 go build -tags gl -o app.wasm .     # WebGL

The web build uses no C code. If your environment sets CGO_ENABLED=1, the build fails in runtime/cgo, which is why the commands above turn it off.

Your page then needs Go’s wasm_exec.js (in $(go env GOROOT)/lib/wasm/) and a script that starts the app. One thing is specific to Bellui: before you call go.run, store the instantiated module in window.wasm. The WebGPU renderer reads the WebAssembly memory through it.

<script src="wasm_exec.js"></script>
<script>
  const go = new Go();
  WebAssembly.instantiateStreaming(fetch("app.wasm"), go.importObject)
    .then((result) => {
      window.wasm = result;      // required by the WebGPU renderer
      go.run(result.instance);
    });
</script>

The index.html that bellui package -os web writes does this already.

WebGPU or WebGL

WebGPU (default)WebGL (-tags gl)
Browser supportBrowsers with WebGPU enabledBrowsers with WebGL
Page requirementA secure context: HTTPS, or http://localhostNone beyond WebGL
StatusVerified in ChromiumThe long-standing renderer

WebGPU is only available to pages in a secure context. bellui serve on localhost counts as secure; a page served over plain HTTP from another address does not, and WebGPU is not available to it. The page that bellui writes then says that a secure page is needed, rather than that the browser lacks WebGPU. Serve production builds over HTTPS, or use the WebGL build if you cannot. To try a build on another device, see Opening the app on a phone.

There is no automatic fallback at run time: the renderer is chosen when you build. To reach browsers without WebGPU, publish the WebGL build.

How the browser changes things

where := "a desktop or mobile app"
if bellui.CurrentDevice().IsBrowser() {
	where = "a web page"
}
label := widget.NewLabel("Running as " + where)

// In the browser a second window opens inside the page's canvas.
more := widget.NewButton("Open another window", func() {
	w2 := a.NewWindow("Second")
	w2.SetContent(widget.NewLabel("Drag me by the title bar"))
	w2.Show()
})

docs, _ := url.Parse("https://bellui.com/docs/")
link := widget.NewHyperlink("Read the docs", docs)
  • bellui.CurrentDevice().IsBrowser() is true.
  • One canvas, several windows. The first window fills the page. Further windows open as windows inside it that the user can drag, but they are not separate browser windows.
  • Files. Opening a file uses the browser’s file picker. Saving offers the file as a download when you close the writer. See File and folder dialogs.
  • Storage. Preferences are kept in the browser’s localStorage, and app storage in IndexedDB.
  • No desktop extras. There is no system tray and there are no global shortcuts.

The complete example

// An app that also runs in the browser: build it with GOOS=js GOARCH=wasm.
package main

import (
	"net/url"

	"github.com/belluikit/bellui"
	"github.com/belluikit/bellui/app"
	"github.com/belluikit/bellui/container"
	"github.com/belluikit/bellui/widget"
)

func main() {
	a := app.NewWithID("com.example.web")
	w := a.NewWindow("Web")

	where := "a desktop or mobile app"
	if bellui.CurrentDevice().IsBrowser() {
		where = "a web page"
	}
	label := widget.NewLabel("Running as " + where)

	// In the browser a second window opens inside the page's canvas.
	more := widget.NewButton("Open another window", func() {
		w2 := a.NewWindow("Second")
		w2.SetContent(widget.NewLabel("Drag me by the title bar"))
		w2.Show()
	})

	docs, _ := url.Parse("https://bellui.com/docs/")
	link := widget.NewHyperlink("Read the docs", docs)

	w.SetContent(container.NewVBox(label, more, link))
	bellui.ShowAndRun(w)
}