Viewport Relay
A self-hosted WebSocket relay that lets viewers reach Viewport daemons on other machines, forwarding end-to-end encrypted traffic it cannot read.
A self-hosted WebSocket relay. Daemons dial out and register a hub, proving they own their key; viewers attach by hub id; the relay forwards end-to-end encrypted bytes it cannot read. Optional: a viewer can always reach a daemon directly.
- Status
- On npm
- Languages
- TypeScript
- License
- Apache 2.0
- Source
- viewportai/relay
A daemon on your laptop or a build machine often has no open port. The relay fixes that. Daemons dial out and register a hub, proving they own their key. Viewers attach by hub id. The relay forwards opaque bytes between them: Noise ciphertext between the viewer's key and the daemon's key, which it never parses.
It is the only network service in Viewport, and it is optional. A viewer can always reach a daemon directly.
What it does
A relay with token admission, one daemon that registers its hub with a real key proof, and one viewer that attaches
by hub id. The daemon and viewer are the test fakes from @viewportai/relay/testing.
import { randomBytes } from "node:crypto";
import { createRelay, tokenAdmission } from "@viewportai/relay";
import { connectDaemon, connectViewer } from "@viewportai/relay/testing";
const token = randomBytes(32).toString("hex"); // a fresh random token per run, like `openssl rand -hex 32`
const relay = createRelay({ admission: tokenAdmission({ token }) });
const { url, origin } = await relay.listen({ port: 0 }); // 127.0.0.1, ephemeral port
const daemon = await connectDaemon(url, { origin, token }); // proves its X25519 key, owns its hub
const viewer = await connectViewer(url, daemon.hub, { token });
await viewer.online();
viewer.send(new TextEncoder().encode("ciphertext from the viewer"));
const { conn, ipHash } = await daemon.nextOpen();
const frame = await daemon.nextFrame();
daemon.send(conn, new TextEncoder().encode("ciphertext from the daemon"));
const reply = await viewer.next();
console.log(`hub ${daemon.hub}`);
console.log(`daemon conn ${conn} opened (ip_hash ${ipHash.slice(0, 12)}...), got "${frame.body}"`);
console.log(`viewer got "${reply.toString()}"`);
console.log(`stats ${JSON.stringify(relay.stats())}`);
viewer.close();
daemon.close();
await relay.close(); // stops the listener; without it the process stays aliveOutput (the hub id and ip_hash differ on every run):
hub vpd_3l7qhqnnqveqeq4fovnaws2mtd
daemon conn 1 opened (ip_hash 4fd4f5f58720...), got "ciphertext from the viewer"
viewer got "ciphertext from the daemon"
stats {"hubs":1,"daemons":1,"pendingDaemons":0,"viewers":1,"connections":2}Install
@viewportai/relay 0.1.1npm install @viewportai/relayA Dockerfile is in the repository, but the image has not been built and verified yet.
Getting started in 5 minutes
-
Start a relay on loopback. The token admits daemons and viewers. It must be at least 16 RFC 7230 token characters.
node dist/bin.js --token "$(openssl rand -hex 32)"viewport-relay 0.1.0 listening on ws://127.0.0.1:7072 (origin ws://127.0.0.1:7072) -
Check it from another terminal:
curl -s localhost:7072/health # {"ok":true} -
Run the examples. Each starts its own relay in process, so none needs the one above or any key:
make examples -
Connect a real daemon. The rebuilt daemon (0.32.0, in its repository) registers its hub with
connectRelay. Pass the relay's URL and token, andsharedToken: truewhen one token admits both roles. With separate tokens, start the relay with--daemon-tokenand--viewer-token.
To run off loopback, set --public-url to the exact URL daemons dial (wss://relay.example, no path). Its origin
binds every registration proof. Every flag has a VIEWPORT_RELAY_* environment variable; node dist/bin.js --help
lists them.
Use cases
-
Reach daemons behind NAT. Daemons only dial out, so a machine with no open port is reachable through one relay you run.
-
Separate operator credentials. Give daemons and viewers different tokens. A viewer token cannot register a hub, and browsers pass the token as a WebSocket subprotocol.
examples/ts/separate-tokens/main.ts const relay = createRelay({ admission: tokenAdmission({ daemons: [daemonToken], viewers: [viewerToken] }) }); const { url, origin } = await relay.listen({ port: 0 }); -
Mount it on a server you already run.
handleUpgradeandhandleRequestattach to any Node HTTP server behind your own TLS terminator, with/health, and Prometheus-style/metricsbehind an admin token.examples/ts/embed-server/main.ts const relay = createRelay({ admission: tokenAdmission({ token }), publicUrl: `ws://127.0.0.1:${port}`, // e.g. wss://relay.example behind your TLS terminator adminToken, }); // handleUpgrade takes over every upgrade it is given (404 for paths that are not the relay's): route by path first. server.on("upgrade", (req, socket, head) => { if (req.url?.startsWith("/v1/daemon") || req.url?.startsWith("/v1/viewer")) relay.handleUpgrade(req, socket, head); else socket.destroy(); // or hand it to your own WebSocket server }); server.on("request", (req, res) => { if (req.url === "/app") return void res.end("your own route\n"); relay.handleRequest(req, res); // /health, /metrics, 404 }); -
Test your own daemon or viewer client.
testRelay()runs a real relay on loopback with fake daemons and viewers that speak the wire contract, including hub replacement on reconnect.
How it fits
| Package | With the relay |
|---|---|
| Daemon | The rebuilt daemon's connectRelay and viewer client are tested against this relay. |
| Console | Pairs daemons through a relay using the daemon's client. |
| Core, Evals, HITL, Context | Not used. |
The relay implements section 6 of the wire contract: registration, the viewer side, auth and limits, and close codes. That contract is the rebuilt daemon's protocol (0.32.0); the published 0.31.x daemon does not speak it. Read Security for what the relay can and cannot see.
Known limits
- No container image yet; the Dockerfile in the repository is not built or verified.
- State lives in memory in one process: a restart drops every connection, and clients reconnect.
- This is the v0.1 rebuild. The earlier Redis-based relay in the archived viewportai/viewport monorepo is no longer documented.
Postmortem: the macOS stop sweep killed unrelated processes
For about 25 minutes on Nov 26 2026, the macOS stop sweep SIGKILLed about 100 to 120 of the owner's own processes each time it ran.
Security
What a Viewport Relay can see, how a daemon owns its hub, what relay tokens grant, and the limits that bound a flood.