Skip to content

Commit 0f6286b

Browse files
authored
feat: implement chat example with manual dial (#5)
* feat: implement chat example with manual dial * feat: implement interactive/echo modes * ci: implement release automation * feat: wait for the relayed interfaces before proceeding with the main flow * feat: resolve relayed dial on the first connection
1 parent f131308 commit 0f6286b

15 files changed

+1414
-2
lines changed
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Build and release chat example
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- feat/chat-example
7+
permissions: write-all
8+
jobs:
9+
metadata:
10+
name: Get release metadata
11+
runs-on: ubuntu-latest
12+
outputs:
13+
version: ${{ steps.get_version.outputs.version }}
14+
release_exists: ${{ steps.check_release.outputs.exists }}
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Get version
20+
id: get_version
21+
run: echo "version=chat-example-$(cargo read-manifest --manifest-path examples/chat/Cargo.toml | jq -r '.version')" >> $GITHUB_OUTPUT
22+
23+
- name: Check if release exists
24+
id: check_release
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
run: |
28+
RELEASE_URL=$(curl --silent "https://api.github.com/repos/calimero-network/relay-server/releases/tags/${{ steps.get_version.outputs.version }}" \
29+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
30+
-H "Accept: application/vnd.github.v3+json" | jq -r '.url')
31+
if [[ "$RELEASE_URL" != "null" ]]; then
32+
echo "exists=true" >> $GITHUB_OUTPUT
33+
else
34+
echo "exists=false" >> $GITHUB_OUTPUT
35+
fi
36+
37+
release:
38+
name: Build and release
39+
runs-on: ubuntu-latest
40+
needs: metadata
41+
if: needs.metadata.outputs.release_exists == 'false'
42+
steps:
43+
- name: Checkout code
44+
uses: actions/checkout@v4
45+
46+
- name: Setup rust toolchain
47+
run: rustup toolchain install stable --profile minimal
48+
49+
- name: Setup rust cache
50+
uses: Swatinem/rust-cache@v2
51+
52+
- name: Build for Intel Linux
53+
run: cargo build -p chat-example --release --target=x86_64-unknown-linux-gnu
54+
55+
- name: Build for Aarch Linux
56+
run: cross build -p chat-example --release --target=aarch64-unknown-linux-gnu
57+
58+
- name: Create artifacts directory
59+
run: |
60+
mkdir -p artifacts
61+
cp target/x86_64-unknown-linux-gnu/release/chat-example artifacts/chat-example-x86_64-unknown-linux
62+
cp target/aarch64-unknown-linux-gnu/release/chat-example artifacts/chat-example-aarch64-unknown-linux
63+
64+
- name: Create GitHub Release
65+
uses: softprops/action-gh-release@v2
66+
with:
67+
tag_name: ${{ needs.metadata.outputs.version }}
68+
files: |
69+
examples/chat/README.md
70+
artifacts/*
71+
env:
72+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Cargo.lock

+79-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = [".", "examples/dcutr"]
2+
members = [".", "examples/dcutr", "examples/chat"]
33

44
[package]
55
name = "relay-server"

examples/chat/Cargo.toml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[package]
2+
name = "chat-example"
3+
version = "0.1.0"
4+
authors = ["Calimero Limited <info@calimero.network>"]
5+
edition = "2021"
6+
repository = "https://github.com/calimero-network/relay-server"
7+
license = "MIT OR Apache-2.0"
8+
9+
[dependencies]
10+
clap = { version = "4.5.4", features = ["derive", "env"] }
11+
eyre = "0.6.12"
12+
libp2p = { version = "0.53.2", features = [
13+
"dcutr",
14+
"dns",
15+
"gossipsub",
16+
"identify",
17+
"macros",
18+
"noise",
19+
"ping",
20+
"quic",
21+
"relay",
22+
"tokio",
23+
"tcp",
24+
"tls",
25+
"yamux",
26+
] }
27+
multiaddr = "0.18.1"
28+
owo-colors = "4.0.0"
29+
serde = "1.0.196"
30+
serde_json = "1.0.113"
31+
tokio = { version = "1.35.1", features = [
32+
"io-std",
33+
"macros",
34+
"rt",
35+
"rt-multi-thread",
36+
] }
37+
tracing = "0.1.37"
38+
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

examples/chat/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Chat
2+
This examples show cases how to manually dial (connect to) either local peer or remote peer has a reservation on relay-server.
3+
4+
## Run local only
5+
This examples shows how to run two sessions locally and connect sessions by manually dialing local peer.
6+
7+
Run first chat session in echo mode.
8+
```
9+
cargo run -p chat-example -- --mode echo --port 4002 --secret-key-seed 102 --gossip-topic-names calimero-network/examples/chat/v0.0.1 --relay-address /ip4/3.71.239.80/udp/4001/quic-v1/p2p/12D3KooWAgFah4EZtWnMMGMUddGdJpb5cq2NubNCAD2jA5AZgbXF
10+
```
11+
12+
Run second chat session in interactive mode with local peer dial.
13+
```
14+
cargo run -p chat-example -- --mode interactive --port 4003 --secret-key-seed 103 --gossip-topic-names calimero-network/examples/chat/v0.0.1 --dial-peer-addrs /ip4/127.0.0.1/udp/4002/quic-v1/p2p/12D3KooWMpeKAbMK4BTPsQY3rG7XwtdstseHGcq7kffY8LToYYKK --relay-address /ip4/3.71.239.80/udp/4001/quic-v1/p2p/12D3KooWAgFah4EZtWnMMGMUddGdJpb5cq2NubNCAD2jA5AZgbXF
15+
```
16+
17+
In the interactive session publish new message manually:
18+
```
19+
publish calimero-network/examples/chat/v0.0.1 ola
20+
```
21+
22+
## Run locally with remote peer dial in
23+
This examples shows how to run two sessions locally and connect sessions manually by dialing private remote peer from each session. For the gossip message to pass from one local session to second local session it needs to go "the long way" around (local -> remote -> local).
24+
25+
Additional info:
26+
- Remote instance is running in a private subnet behind NAT.
27+
- Remote instance PeerId: `12D3KooWP285Hw3CSTdr9oU6Ezz4hDoi6XS5vfDjjNeTJ1uFMGvp`
28+
- Remote instance address at the relay server: `ip4/3.71.239.80/udp/4001/quic-v1/p2p/12D3KooWAgFah4EZtWnMMGMUddGdJpb5cq2NubNCAD2jA5AZgbXF/p2p-circuit/p2p/12D3KooWP285Hw3CSTdr9oU6Ezz4hDoi6XS5vfDjjNeTJ1uFMGvp`
29+
Run first chat session in interactive mode with remote peer dial.
30+
```
31+
cargo run -p chat-example -- --mode interactive --port 4002 --secret-key-seed 102 --gossip-topic-names calimero-network/examples/chat/v0.0.1 --dial-peer-addrs /ip4/3.71.239.80/udp/4001/quic-v1/p2p/12D3KooWAgFah4EZtWnMMGMUddGdJpb5cq2NubNCAD2jA5AZgbXF/p2p-circuit/p2p/12D3KooWP285Hw3CSTdr9oU6Ezz4hDoi6XS5vfDjjNeTJ1uFMGvp --relay-address /ip4/3.71.239.80/udp/4001/quic-v1/p2p/12D3KooWAgFah4EZtWnMMGMUddGdJpb5cq2NubNCAD2jA5AZgbXF
32+
```
33+
34+
Run second chat session in interactive mode with remote peer dial.
35+
```
36+
cargo run -p chat-example -- --mode interactive --port 4003 --secret-key-seed 103 --gossip-topic-names calimero-network/examples/chat/v0.0.1 --dial-peer-addrs /ip4/3.71.239.80/udp/4001/quic-v1/p2p/12D3KooWAgFah4EZtWnMMGMUddGdJpb5cq2NubNCAD2jA5AZgbXF/p2p-circuit/p2p/12D3KooWP285Hw3CSTdr9oU6Ezz4hDoi6XS5vfDjjNeTJ1uFMGvp --relay-address /ip4/3.71.239.80/udp/4001/quic-v1/p2p/12D3KooWAgFah4EZtWnMMGMUddGdJpb5cq2NubNCAD2jA5AZgbXF
37+
```
38+
39+
In any interactive session publish new message manually:
40+
```
41+
publish calimero-network/examples/chat/v0.0.1 ola
42+
```
43+
44+
## Debugging and known issues
45+
- If multiple people are running the same example, some will fail to get reservation on relay server because the same PeerId already exists.
46+
- Fix: change `secret-key-seed` to something else
47+

0 commit comments

Comments
 (0)