Files
BridgeSwarm/docs/HRPC.md
T
Raven Scott f4569b765e
CI / Build & Test (push) Successful in 3m12s
Updates
2026-07-26 22:58:33 -04:00

56 lines
1.8 KiB
Markdown

# HRPC on the Native Host
The native host runs **[hrpc](https://www.npmjs.com/package/hrpc)** over a connection (schema-defined RPC on a Protomux channel). The browser does not run hrpc itself; it attaches HRPC on the host and invokes methods via `hrpcInvoke` / `swarm.hrpcCall`.
## Enabling HRPC
```javascript
await BridgeSwarm.request('attachHrpc', { connId: conn.connId });
```
Both peers must attach HRPC on the same connection pair. The first invoke may wait up to **12s** for the channel to open.
## Commands (spec)
| Command | Mode | Request | Response |
|---------|------|---------|----------|
| `ping` | Unary | `{ value? }` | `{ pong }` |
| `streamSum` | Request-stream → response | chunks `{ n, label }` (pass `args.chunks` from browser) | `{ sum, count }` on `hrpc-end` |
| `fetchStream` | Request → response-stream | `{ count }` | chunks `{ i, data }` via `hrpc-chunk` |
| `duplex` | Duplex | `args.chunks` `{ x, y }` | chunks `{ result, n }` via `hrpc-chunk` |
| `notify` | Send-only | `{ event, payload }` | none |
Build: `npm run build:hrpc``spec/hrpc/` + `native-host/spec/hrpc/`.
## Invoking from the browser
### Unary / notify
```javascript
const res = await swarm.hrpcCall(conn.connId, 'ping', { value: 'hello' });
// or: BridgeSwarm.request('hrpcInvoke', { connId, method: 'ping', args })
```
### Streaming
```javascript
const res = await swarm.hrpcCall(conn.connId, 'fetchStream', { count: 3 }, {
onChunk: (chunk) => console.log(chunk),
timeoutMs: 20000,
});
// Host replies immediately with { streaming: true, streamId }, then emits
// hrpc-chunk / hrpc-end / hrpc-error events to the page.
```
```javascript
await swarm.hrpcCall(conn.connId, 'streamSum', {
chunks: [{ n: 1, label: 'a' }, { n: 2, label: 'b' }],
});
```
## See also
- [DATA-API.md](DATA-API.md)
- [API-REFERENCE.md](API-REFERENCE.md)
- `examples/hrpc-demo/`