Pair & Send Example
This example demonstrates how a secondary device can connect to Relayly, accept a pairing code generated by another device, send a single encrypted message, wait for a reply, and then exit.
It’s the perfect companion to the Basic Echo Example.
Prerequisites
- A running Relayly server (e.g., via
docker compose up -d) - Go ≥ 1.24
- A pairing code generated by another device (like the Basic Echo example)
Usage
Navigate to the examples/go/pair-and-send directory in the Relayly repository:
cd examples/go/pair-and-send Run the example, providing the server URL and the 6-digit pairing code as arguments. Replace 483921 with the actual code from your other device.
go run . ws://localhost:8080/ws 483921 Expected Output
The application will connect, authenticate, accept the pairing, and send a message:
Accepting pair code 483921...
✓ Paired with: my-laptop
✓ Sent: "Hello from Go! 👋"
Waiting for reply (5 seconds)...
✓ Reply from my-laptop: echo: Hello from Go! 👋 After printing the reply (or timing out after 5 seconds), the program will exit.
Code Overview
The main.go file demonstrates the sequence for connecting and sending data:
- Connection: Uses
relayly.Connect()to establish the connection and authenticate. - Pairing Acceptance: Calls
client.AcceptPair(ctx, pairCode)with the code provided in the command line argument. This securely completes the pairing process. - Sending: Uses
client.Send()to send a single encrypted message to the paired peer. - Waiting for Reply: Listens on
client.Messages()with a 5-second timeout to receive the echo response.
Check out the full source code in examples/go/pair-and-send.