TypeScript Echo (Node.js)

A minimal TypeScript example that connects to a Relayly relay, pairs with another device, and echoes every message back with an Echo: prefix. Good for testing connectivity and as a starting point for your own TypeScript integration.

Prerequisites

  • A running Relayly server
  • Node.js 18 or newer
  • The relayly npm package

Setup

cd examples/ts/echo
npm install

Usage

Device A — start the echo server and get a pairing code:

npx tsx index.ts

Output:

Connecting to ws://localhost:8080 as echo-node-12345…
Connected.

  Share this code with your other device:
  483921

  Or run: npx tsx index.ts --code 483921

Waiting for peer to connect…

Device B — connect with the code:

npx tsx index.ts --code 483921

Once paired, any message sent from the other device is echoed back:

[echo-node-67890] Hello!
[echo] Echo: Hello!

Configuration

The relay URL defaults to ws://localhost:8080. Override it with the RELAYLY_URL environment variable:

RELAYLY_URL=wss://relay.example.com npx tsx index.ts

The device key is stored at ~/.relayly/echo.key and reused across runs.

Code overview

The example shows the core Relayly TypeScript pattern:

  1. Load or generate a persistent keypair from disk
  2. Connect with new RelaylyClient(url, { deviceId, keyPair })
  3. Either call requestPairCode() or acceptPair(code) depending on which side starts first
  4. Listen for messages with client.on('message', handler) and reply with client.send()

Check out the full source at examples/ts/echo.