Node.js Send
A TypeScript one-shot example that accepts a pairing code, sends a single encrypted message to the paired device, waits up to 5 seconds for a reply, then exits. Works well as the sender side paired with the TypeScript Echo or Basic Echo examples.
Prerequisites
- A running Relayly server
- Node.js 18 or newer
- A pairing code from another running device (e.g., the echo example)
Setup
cd examples/ts/node
npm install Usage
npx tsx send.ts <server-url> <pair-code> [message] For example, if the echo device printed code 483921:
npx tsx send.ts ws://localhost:8080 483921 "Hello from Node.js!" Output:
Connecting to ws://localhost:8080…
Connected
Accepting pair code 483921…
Paired with: echo-node-12345
Sent: "Hello from Node.js!"
Waiting for reply (5s)…
Reply: "Echo: Hello from Node.js!" Configuration
You can also use environment variables instead of CLI arguments:
| Variable | Description |
|---|---|
RELAYLY_SERVER | Relay server URL |
RELAYLY_PAIR_CODE | 6-digit pairing code |
RELAYLY_MESSAGE | Message to send (default: Hello from Node.js! 👋) |
RELAYLY_KEY_PATH | Path to persist the device key |
RELAYLY_SERVER=wss://relay.example.com
RELAYLY_PAIR_CODE=483921
npx tsx send.ts Code overview
- Load or generate a persistent keypair (stored at
~/.relayly/node-device.key) - Connect with
new RelaylyClient(serverUrl, { deviceId, keyPair }) - Call
client.acceptPair(pairCode)to complete the pairing - Send the message with
client.send(peer.id, message) - Wait for a reply with a 5-second timeout using
Promise.race
Check out the full source at examples/ts/node.