Skip to content

Commit 931719c

Browse files
committed
Improve readmes
1 parent a7ee48e commit 931719c

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

examples/worker-basic/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Interactive playground for `@jmorrell/jsonrpc` on Cloudflare Workers.
44

55
Defines a handful of toy RPC methods on the server and serves a static page
66
where you can invoke them individually or in bulk. A toggle switches between
7-
**HTTP batch** and **WebSocket** transports — the server code is identical
7+
**HTTP batch** and **WebSocket** transports. The server code is identical
88
because `newWorkersRpcResponse` routes both automatically.
99

1010
## Running

examples/worker-bidirectional/README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22

33
Bidirectional WebSocket RPC with a Durable Object audit log.
44

5-
The server defines the same toy methods as `worker-basic`, but each call also
6-
broadcasts an audit event to every connected client via `onEvent()` — a method
7-
the _server_ calls on the _client_. Open the page in two browser tabs and watch
8-
events from one tab appear in the other.
5+
We have a bidirectional RPC implementation, so we should use it, right?
6+
7+
This demo has the server invoking remote methods on the client. Each call
8+
on the server emits an event to the client.
9+
10+
It's worth calling out that you likely need some centralized state with this
11+
approach. A worker connected via a websocket could work in theory in that it
12+
can respond to requests coming in over the same connection. However if you have
13+
multiple sessions per user (think multiple tabs, or a desktop or mobile app) you
14+
likely want to send those requests to every session for a particular user.
15+
16+
The best way to do that is a Durable Object. This demo app gives an example. Open
17+
the page in two browser tabs and watch events from one tab appear in the other.
918

1019
## Setting up a bi-directional WebSocket RPC session
1120

12-
The main difference between this example and `worker-basic` is that we're explicitly setting up a bi-directional WebSocket RPC session with `newWorkersWebSocketRpcSession`. This returns the upgrade
13-
response and an `RpcSession` — use `session.remote` to call methods on the client, and
14-
`session.onClose()` to clean up when the connection drops.
21+
The main difference between this example and `worker-basic` is that we're explicitly setting up a bi-directional WebSocket RPC session with `newWorkersWebSocketRpcSession`. This returns the upgrade response and an `RpcSession`. Use `session.remote` to call methods on the client, and `session.onClose()` to clean up when the connection drops.
1522

1623
```ts
1724
// Set up bidirectional RPC with the client
@@ -48,7 +55,7 @@ Open http://localhost:8787.
4855

4956
```
5057
src/worker.ts Worker + AuditLog Durable Object
51-
web/src/app.ts Client bidirectional RPC with onEvent handler
58+
web/src/app.ts Client - bidirectional RPC with onEvent handler
5259
web/src/style.css Styles (two-column layout)
5360
web/index.html Vite entry point
5461
web/vite.config.ts Aliases @jmorrell/jsonrpc to the local dist build

0 commit comments

Comments
 (0)