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
relaylynpm 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:
- Load or generate a persistent keypair from disk
- Connect with
new RelaylyClient(url, { deviceId, keyPair }) - Either call
requestPairCode()oracceptPair(code)depending on which side starts first - Listen for messages with
client.on('message', handler)and reply withclient.send()
Check out the full source at examples/ts/echo.