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:

VariableDescription
RELAYLY_SERVERRelay server URL
RELAYLY_PAIR_CODE6-digit pairing code
RELAYLY_MESSAGEMessage to send (default: Hello from Node.js! 👋)
RELAYLY_KEY_PATHPath to persist the device key
RELAYLY_SERVER=wss://relay.example.com 
RELAYLY_PAIR_CODE=483921 
npx tsx send.ts

Code overview

  1. Load or generate a persistent keypair (stored at ~/.relayly/node-device.key)
  2. Connect with new RelaylyClient(serverUrl, { deviceId, keyPair })
  3. Call client.acceptPair(pairCode) to complete the pairing
  4. Send the message with client.send(peer.id, message)
  5. Wait for a reply with a 5-second timeout using Promise.race

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