toip.tools

function · Node only

startRelay

Opens a SQLite-backed WebSocket mailbox at /v1 and starts listening for authenticated clients. Messages remain stored until acknowledged or expired; the relay sees routing metadata, not plaintext.

@toip-tools/relayView source
TypeScriptImport
import { startRelay } from "@toip-tools/relay";

Declaration

TypeScriptSignature
startRelay(opts: RelayOptions): Promise<Relay>;

Parameters

optsrequired
RelayOptions

Bind address, database path, public authentication URL, queue limits, and optional observers. Use port: 0 for an assigned port. publicUrl must exactly match the URL clients sign during authentication.

Fields of opts
portrequired
number
dbPathrequired
string
publicUrloptional
string | undefined

The URL clients use, e.g. wss://relay.example.com/v1. Defaults to ws://:/v1, or ws://localhost:/v1 when bound to every interface.

hostoptional
string | undefined
logoptional
((line: string) => void) | undefined
limitsoptional
Partial<RelayLimits> | undefined
onStoredoptional
((m: StoredMessage) => void) | undefined

Called for every message the relay accepts: what an operator can see (sender, recipient, size, time), never the contents.

Returns

Promise<Relay>

A listening relay with its actual port, advertised URL, queue counter, and asynchronous close method. The caller must await close() during shutdown to terminate sockets, stop expiry sweeps, and close SQLite.

Throws

If opening or initializing SQLite fails, or the WebSocket server cannot start listening.