Realtime / Private stream
Private streams. One Durable Object per user.
This is a working gateway demo: the browser opens a private WebSocket, the Worker validates the session, and a Durable Object broadcasts order and fill events. The admin publish path stays server-side.
WebSocket at the edge
The Worker accepts the upgrade and routes by user address. The Durable Object owns the connection.
Hibernation-friendly
Client sockets are accepted with ctx.acceptWebSocket, so idle sessions can hibernate.
Admin stays server-side
The browser calls /api/gateway/publish; the Next server attaches the admin token.
Live demo
Connect and publish a fill
worker + durable object · port 8788
Demo token: demo-token
Publish goes through Next.js so the admin token stays server-side.
Connect, then send a test fill. Events arrive through the Durable Object.
Start the gateway with npm run gateway:dev, then click Connect and Test fill. Without the gateway the demo shows an offline state; the rest of the site is unaffected.
Production path
What changes for real users
- • Replace the demo token with a short-lived signed session token.
- • Verify account ownership with SIWE or your auth service.
- • Store the admin token with Wrangler secrets, never in the client.
- • Add per-user rate limits and idempotency keys.
- • Keep order signing client-side or in a secure backend.
- • Do not put exchange master keys in a shared Worker.
Boundary
What belongs where
| Concern | Place |
|---|---|
| Public market stream | Browser → exchange WSS |
| Private fills / orders | Worker + DO per user |
| Order signing | Client or secure backend |
| Event fan-out | Durable Object |
| Admin publish | Server-side route |