Skip to content

Commit 2910c8c

Browse files
jmorrellclaude
andauthored
Add examples (#10)
## Summary - Adds `worker-basic` example: HTTP batch + WebSocket transport toggle with interactive playground - Adds `worker-bidirectional` example: server-to-client RPC with Durable Object audit log broadcast - Returns `RpcSession` from WebSocket helpers instead of a proxy, giving callers access to `session.onClose()` for connection lifecycle management - Adds `onClose(handler)` and `Symbol.dispose` to `RpcSession` ## API changes `newWorkersWebSocketRpcSession` now returns `{ response, session }` instead of `{ response, remote }`: ```ts const { response, session } = newWorkersWebSocketRpcSession<ClientApi, typeof service>( request, service, ); session.remote.onEvent(event); // call methods on the client session.onClose(() => cleanup()); // clean up when connection drops ``` `newWebSocketRpcSession` now returns an `RpcSession` instead of a proxy: ```ts const session = newWebSocketRpcSession<ServerApi>(wsUrl); await session.remote.add(1, 2); // was: await proxy.add(1, 2) session.close(); ``` ## Test plan - [x] All 163 existing tests pass - [x] New tests for `onClose` (transport close, session close, already-closed, multiple handlers) - [x] New test for `Symbol.dispose` on `RpcSession` - [ ] Manual: run `worker-bidirectional` example, open two tabs, verify audit events broadcast, close a tab and verify subscription cleanup via `onClose` --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5a8749a commit 2910c8c

34 files changed

Lines changed: 7149 additions & 52 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ repos
22
node_modules
33
dist
44
.claude
5+
.wrangler
6+
.DS_Store

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Last verified: 2026-02-16
2929
- `src/core.ts` - Shared types, error classes, and transport-agnostic JSON-RPC 2.0 engine (wire format types, type guards, request/response builders, RpcProtocolError, RPC processor)
3030
- `src/http-batch.ts` - HTTP batch transport: newHttpBatchRpcResponse (server) + newHttpBatchRpcSession (client with auto-batching)
3131
- `src/session.ts` - Bidirectional RPC over message transports (defines session-specific types: RpcTransport, RpcSessionOptions, RpcSession)
32-
- `src/websocket.ts` - WebSocket transport: newWorkersWebSocketRpcResponse (server, fire-and-forget) + newWorkersWebSocketRpcSession (server, bidirectional with typed remote proxy) + newWebSocketRpcSession (client with Disposable proxy)
32+
- `src/websocket.ts` - WebSocket transport: newWorkersWebSocketRpcResponse (server, fire-and-forget) + newWorkersWebSocketRpcSession (server, returns {response, session} for bidirectional RPC) + newWebSocketRpcSession (client, returns RpcSession)
3333
- `src/__tests__/` - Test files
3434
- `src/__tests__/fixtures/worker.ts` - Test worker entry point for Workers runtime tests
3535
- `src/__tests__/fixtures/wrangler.toml` - Minimal Workers config for test worker

examples/worker-basic/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# worker-basic
2+
3+
Interactive playground for `@jmorrell/jsonrpc` on Cloudflare Workers.
4+
5+
Defines a handful of toy RPC methods on the server and serves a static page
6+
where you can invoke them individually or in bulk. A toggle switches between
7+
**HTTP batch** and **WebSocket** transports — the server code is identical
8+
because `newWorkersRpcResponse` routes both automatically.
9+
10+
## Running
11+
12+
```bash
13+
# from repo root — the example links to the local library build
14+
npm run build
15+
16+
# install and build the example
17+
cd examples/worker-basic
18+
npm install
19+
cd web && npm install && npm run build && cd ..
20+
21+
# start the dev server
22+
npm run dev
23+
```
24+
25+
Open http://localhost:8787.
26+
27+
## Structure
28+
29+
```
30+
src/worker.ts Cloudflare Worker — defines the service and routes /api
31+
web/src/app.ts Client — uses newHttpBatchRpcSession or newWebSocketRpcSession
32+
web/src/style.css Styles
33+
web/index.html Vite entry point
34+
web/vite.config.ts Aliases @jmorrell/jsonrpc to the local dist build
35+
```
36+
37+
## What it demonstrates
38+
39+
- **Defining a service** — plain object with typed methods, passed to
40+
`newWorkersRpcResponse`.
41+
- **HTTP auto-batching** — calls made in the same tick are combined into a
42+
single POST containing a JSON-RPC batch array.
43+
- **WebSocket transport** — persistent connection, each call is an individual
44+
JSON-RPC message (no batching needed).
45+
- **Type-safe client**`newHttpBatchRpcSession<Api>` and
46+
`newWebSocketRpcSession<Api>` return a typed proxy where method calls map
47+
directly to RPC requests.
48+
- **Raw protocol visibility** — every call shows the JSON-RPC request and
49+
response side by side.

0 commit comments

Comments
 (0)