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/relay` · [View source](https://github.com/yourbuddyconner/toip-tools/blob/main/packages/relay/src/server.ts#L91)

Import:

```ts
import { startRelay } from "@toip-tools/relay";
```

## Declaration

Signature:

```ts
startRelay(opts: RelayOptions): Promise<Relay>;
```

### Parameters

**`opts` (required)**

`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`**

**`port` (required)**

`number`

**`dbPath` (required)**

`string`

**`publicUrl` (optional)**

`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.

**`host` (optional)**

`string | undefined`

**`log` (optional)**

`((line: string) => void) | undefined`

**`limits` (optional)**

`Partial<RelayLimits> | undefined`

**`onStored` (optional)**

`((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.

[Usage guide](https://toip.tools/toolkit/relay/#usage) · [Error handling](https://toip.tools/toolkit/relay/#errors) · [All exports](https://toip.tools/toolkit/relay/api/)

---

This is the Markdown version of https://toip.tools/toolkit/relay/api/startRelay/. An index of the documentation for AI assistants is at https://toip.tools/llms.txt.
