In the browser
The @glowcap/web package runs glowcap’s WebAssembly protocol core. A browser uses the same Session operations as the TypeScript binding. JavaScript supplies the byte channel and scheduling.
Start with the browser bundle
Download the v0.1.7 browser bundle, extract it into a new directory your web server serves, and open index.html. It includes JavaScript, matching Wasm, and a complete WebRTC greeter example. The package README has local serving commands.
Combine a transport tab below with the fetch-and-call block beneath it. Each example needs a running remote peer. WebRTC also needs your application’s authenticated signaling. Use the bundle’s example.js for a complete local starting point.
Dial
The paths below open outbound sessions. A page can also accept sessions through WebRTC or a registered relay route, as described under Accept peers.
irohreaches an iroh node id through the relay thatinit‘srelayUrloption names, overwss://. On this path the relay ends the QUIC connection, so it can read traffic and vouches for the peer’s identity.webrtcreaches a peer over a WebRTC DataChannel. It needs the@glowcap/webrtcconnector installed.noise-relayreaches a peer registered at the relay. The Noise handshake seals the stream, so the relay forwards only ciphertext. It needs the@glowcap/relayconnector.keyed-relaytakes the same path through the same connector but sends CapTP frames in the clear.keyed-tcpnames a key at a host and port and sends frames in the clear. The built-in runtime opens it through WebSocket.tcpis the cleartext testing path, which the built-in runtime also opens through WebSocket.
iroh
import { init } from "@glowcap/web";
const web = await init({
wasm: fetch("/glowcap_host.wasm"),
relayUrl: (locator) => `wss://relay.example/relay?locator=`,
});
await using runtime = web.runtime;
// Copy the server's full URI, including direct-addresses reachable from the relay.
const peerUri = "ocapn://<64 lowercase hex characters>.iroh?direct-addresses=127.0.0.1%3A5000";
const partner = web.parseLocator(peerUri);
webrtc
import { connector, locator, type SignalingAuthority } from "@glowcap/webrtc";
import { init } from "@glowcap/web";
declare const signaling: SignalingAuthority;
const web = await init({
wasm: fetch("/glowcap_host.wasm"),
connectors: [connector(signaling)],
});
await using runtime = web.runtime;
const partner = locator("<64 lowercase hex characters from the peer>");
noise-relay
import { connector, noise } from "@glowcap/relay";
import { init } from "@glowcap/web";
const relayUrl = "wss://relay.example/relay";
const web = await init({
wasm: fetch("/glowcap_host.wasm"),
connectors: [connector()],
});
await using runtime = web.runtime;
const partner = noise.locator("<64 lowercase hex characters>", relayUrl);
keyed-tcp
import { keyed } from "@glowcap/tcp";
import { init } from "@glowcap/web";
const web = await init({
wasm: fetch("/glowcap_host.wasm"),
// A page cannot open raw TCP, so a relay carries the bytes.
wsUrl: (host, port) => `ws://127.0.0.1:9000/relay?to=:`,
});
await using runtime = web.runtime;
// keyed-tcp is cleartext through the relay. Use WebRTC for a sealed browser path.
const partner = keyed.locator("<64 lowercase hex characters>", "127.0.0.1", 9099);
keyed-relay
import { connector, keyed } from "@glowcap/relay";
import { init } from "@glowcap/web";
const relayUrl = "wss://relay.example/relay";
const web = await init({
wasm: fetch("/glowcap_host.wasm"),
connectors: [connector()],
});
await using runtime = web.runtime;
const partner = keyed.locator("<64 lowercase hex characters>", relayUrl);
tcp
import { locator } from "@glowcap/tcp";
import { init } from "@glowcap/web";
const web = await init({
wasm: fetch("/glowcap_host.wasm"),
// A page cannot open raw TCP, so a relay carries the bytes.
wsUrl: (host, port) => `ws://127.0.0.1:9000/relay?to=:`,
});
await using runtime = web.runtime;
const partner = locator("127.0.0.1", 9099);
Fetch the greeter and call its greet method:
await using session = await runtime.connect(partner);
const greeter = await session.fetch("greeter"); // a capability this peer published
const reply = await session.send(greeter, "greet", ["world"]);
console.log(reply);
If you have the key and address separately, import keyed from @glowcap/tcp before init and call keyed.locator. The locator package does not load a compiled library or depend on the initialized wasm adapter.
init instantiates the bundle’s glowcap_host.wasm and hands back the configured browser runtime. From there connect, fetch, send, spawn, and publish work as on the TypeScript page.
Browser dial paths
To reach a non-browser peer, the page dials over WebSocket. A host-and-port dial opens ws://host:port/ by default, straight to a peer that accepts WebSocket itself. For a peer that only speaks raw TCP or iroh, dial through a relay: init‘s wsUrl option maps a host-and-port dial to the relay’s URL, and relayUrl does the same for a locator only a non-browser peer can serve, such as iroh. The CapTP handshake still runs between the browser and the far peer. The relay’s role in authentication and encryption depends on the transport, as described above.
To reach another browser, open a WebRTC DataChannel. A browser registered at a relay can also be dialed there by its key.
Accept peers
Use the WebRTC listener to answer offers from your signaling service. The browser runtime exports serveRendezvous to accept sessions through a relay. Choose noise-relay to keep those sessions encrypted through it.
Next
The TypeScript page covers the shared Session API. The @glowcap/web README covers installing the package and serving the wasm.