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, registering itself via POST /api/v1/devices on first run and reusing the saved credentials afterward.

go run . --server ws://localhost:8080/ws --device-name=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: http://localhost:8080/pair?code=483921

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. Registration: Registers via POST /api/v1/devices on first run (or reuses the saved device_id/device_token).
  3. Connection: Uses relayly.Connect() to establish the secure WebSocket tunnel.
  4. Pairing: Calls client.RequestPairCode() to generate a pairing code for another device to use.
  5. Message Loop: Iterates over client.Messages() and responds to each message using client.Send().

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