Basic Echo Example

This example demonstrates the simplest possible Relayly usage using the Go SDK. It connects to the server, generates a pairing code, and enters an echo loop where it sends back any messages it receives.

Prerequisites

  • A running Relayly server (e.g., via docker compose up -d)
  • Go ≥ 1.24

Usage

Navigate to the examples/go/basic directory in the Relayly repository:

cd examples/go/basic

Run the example. It will load configuration from flags, environment variables, or defaults.

go run . --server=ws://localhost:8080/ws --device-id=my-laptop

Expected Output

The application will connect and print a pairing code:

╔══════════════════════════════════╗
║  Pairing code:  483921            ║
╚══════════════════════════════════╝
Share this code with your other device.
QR URL: ws://localhost:8080/ws/qr/...

Waiting for peer to connect...

At this point, you can use the Pair & Send example on another terminal to connect to it.

Once the other device connects, it will output:

✓ Paired with device: my-phone

Listening for messages (will echo back). Ctrl+C to quit.

Any message received from the paired device will be automatically echoed back.

Code Overview

The main.go file shows how easy it is to integrate Relayly:

  1. Initialization: Loads or generates a persistent device key.
  2. Connection: Uses relayly.Connect() to establish the secure WebSocket tunnel.
  3. Pairing: Calls client.RequestPairCode() to generate a pairing code for another device to use.
  4. Message Loop: Iterates over client.Messages() and responds to each message using client.Send().

Check out the full source code in examples/go/basic.