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 before opening this example.
How to run
npm run examples
Open http://127.0.0.1:4173/hrpc-demo/ (two tabs for P2P). Do not use file://.
Usage
- Click Join topic (default
bridge-swarm-hrpc-demo). - Open the same page in another tab and join the same topic.
- 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.
- 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 — Page API and host request types.
- HRPC on the native host — attachHrpc, commands, and build.