Files
BridgeSwarm/examples/hrpc-demo/README.md
T
Raven Scott 9810f68cb3
CI / Build & Test (push) Successful in 8m20s
Update Docs
2026-07-26 23:53:34 -04:00

46 lines
1.9 KiB
Markdown

# HRPC Demo — Unary RPC (ping/pong) over a connection
## What this example does
HRPC on a connection: HRPC is auto-enabled when a peer connects. Either tab can use "Ping peer" to get a pong (unary RPC). No manual attach step in the UI after connect.
## Prerequisites
Install the [BridgeSwarm extension and native host](../../README.md#easy-install-recommended) before opening this example.
## How to run
**Recommended:** Extension Settings → enable **Examples server**, then open the demo URL below.
Dev alternative from the repo root:
```bash
npm run examples
```
Open **http://127.0.0.1:4173/hrpc-demo/** (two tabs for P2P). Do not use `file://`.
## Usage
1. Click **Join topic** (default `bridge-swarm-hrpc-demo`).
2. Open the same page in another tab and join the same topic.
3. When connected, HRPC is enabled automatically; use **Ping peer** (optionally enter a value). Use **Retry HRPC on selected connection** if the first enable is still pending.
4. Click **Leave and destroy** when done.
## Files in this directory
- **index.html** — Page structure (topic, join/leave, connection selector, HRPC retry, ping input/button, log).
- **style.css** — Layout and theme (sections, badges, log).
- **app.js** — BridgeSwarm setup, auto attachHrpc on connection, hrpcInvoke(ping), connection selector and UI state.
## APIs and concepts
- Swarm/connection as in chat: BridgeSwarm.ready(), new BridgeSwarm(), swarm.join(), swarm.on('connection'), conn.connId, conn.on('end'), conn.on('error').
- BridgeSwarm.request('attachHrpc', { connId }) — Enable HRPC on the connection (called automatically on each new connection).
- BridgeSwarm.request('hrpcInvoke', { connId, method: 'ping', args }) — Invoke the unary ping RPC; host runs HRPC on the connection, browser only invokes ping.
## See also
- [API reference](../../docs/API-REFERENCE.md) — Page API and host request types.
- [HRPC on the native host](../../docs/HRPC.md) — attachHrpc, commands, and build.