starfold

Browser client

The page the server serves, and mount() to show a starfold desktop inside a page of your own.

The server's own page

Every server serves its client at /: open the link it prints. The page takes these query parameters:

Parameter
?token=<password>signs you in; the page passes it on to its own connections, so the link works as an iframe's src
?statsan FPS overlay
?present=canvaspaint into a canvas instead of a <video>
?framed=1for a server started with --framed

A page that embeds it in an iframe can wait for the starfold:firstframe event on window rather than load.

Embedding with mount()

@starfold/web is the same client as an ES module, to mount in an element of another page. It keeps the picture, keyboard and pointer input, pasting into the desktop, the cursor, read-only display and (opt-in) audio; it has no toolbar, files, microphone or resizing.

npm install @starfold/web
bun add @starfold/web
import { mount } from '@starfold/web';

const desktop = mount(document.getElementById('desktop')!, {
	connect: {
		url: 'wss://desktop.example.com/ws',
		preamble: async () => await fetchTicket(),
	},
	reconnect: true,
	onStatus: (status) => console.log(status),
	onClose: (code, reason) => console.log(code, reason),
});

// later
desktop.unmount();

mount() throws on a page that isn't a secure context (HTTPS or localhost) or in a browser without OffscreenCanvas. The server has to let the page's site connect: start it with --allowed-origins naming that site (see Auth and network).

Options

Option
connect.urlthe WebSocket URL, used exactly as given (no query string is added)
connect.protocolsWebSocket subprotocols
connect.preambletext message(s) sent first on every connection, before anything of starfold's, such as a ticket; called before every connect, never logged or passed to a callback
assetBasethe directory with worker.js, dav1d.js, dav1d.wasm and audio.js; default: the module's own directory. It must be on the page's own origin
audioplay the desktop's audio, after the first user gesture; default off
reconnecttrue for the defaults, or an object with limits (below); default off
onStatus(status, detail?)connecting, open, closed, preamble-failed, connect-failed, reconnecting, gave-up
onClose(code, reason)once per connection whose socket was asked for, with the close frame's code and reason
onReconnect(event)with reconnect: reconnecting, reconnected and gave-up events

mount() returns a handle with connect() (connect again; while waiting to reconnect, connect now) and unmount() (stop at once and remove everything mount() added).

Reconnecting

With reconnect, a close worth another attempt calls the same connect() again, the preamble too, so a ticket is minted for each attempt. The delay before attempt n is a random time below min(capMs, initialMs × factor^(n-1)); a run gives up after maxAttempts attempts or maxMs since its first close, whichever comes first.

LimitDefault
initialMs500
factor2
capMs30000
maxAttempts10
maxMs300000 (5 min, on the wall clock)
shouldReconnect(code, reason)the table below
Close codeSent byReconnects
1006 (no close frame)the browser: a network drop, a server that crashed or was killed, or a refused upgradeyes; three in a row that never reached open give up
1001, 1012, 1013a server or proxy going away, restarting, or busyyes
44xx (but 4406)the host's gateway or a serveryes; the same code twice in a row, each within 10 s of its open, gives up
4406starfold: a protocol 1 viewer when the base stream is not H.264no
1000a server or proxy closing on purposeno
1003, 1007, 1009starfold: a frame the client should not have sentno
4000, 4001, 4002the client itself: a protocol error, no common codec, a failed workerno
any otherno

A connection that stays open for 10 s ends the run. A hidden tab waits to be visible again and an offline page for the network. A starfold server that shuts down sends 1012 to every viewer, so viewers with reconnect come back once it restarts.

On this page