Based on https://github.com/nnevatie/capnwebcpp

Improve readability of READMEs

Changed files
+207 -168
examples
batch-pipelining
bluesky
helloworld
serverpush
+14 -4
README.md
··· 10 - **Static File Serving**: Serve static files with proper MIME types and security checks 11 - **Goroutine-Safe**: Thread-safe session management with proper synchronization 12 13 - ## Status 14 - 15 - Production-ready Go implementation of the Cap'n Web RPC protocol. This is a complete rewrite of the original C++ implementation in Go. 16 - 17 ## Dependencies 18 19 - Go 1.21+ ··· 61 3. **Server Push** 62 ```bash 63 cd examples/serverpush 64 go run main.go 65 ``` 66
··· 10 - **Static File Serving**: Serve static files with proper MIME types and security checks 11 - **Goroutine-Safe**: Thread-safe session management with proper synchronization 12 13 ## Dependencies 14 15 - Go 1.21+ ··· 57 3. **Server Push** 58 ```bash 59 cd examples/serverpush 60 + go run main.go 61 + ``` 62 + 63 + In a second tab: 64 + ```bash 65 + cd static 66 + npm install 67 + npm run dev 68 + ``` 69 + Open: http://localhost:3000 70 + 71 + 4. **Bluesky** 72 + ```bash 73 + cd examples/bluesky 74 go run main.go 75 ``` 76
+135
examples/batch-pipelining/README.md
···
··· 1 + # Batch Pipelining Example 2 + 3 + This example demonstrates the power of Cap'n Web RPC's batch pipelining feature by showing how multiple dependent RPC calls can be executed in a single HTTP request. 4 + 5 + ## What This Demo Shows 6 + 7 + - **Batch Pipelining**: Multiple dependent RPC calls in a single HTTP request 8 + - **Pipeline References**: Using the result of one call as input to subsequent calls 9 + - **Performance Comparison**: Side-by-side comparison of pipelined vs sequential execution 10 + - **Network Latency Simulation**: Configurable simulated RTT to demonstrate real-world benefits 11 + - **Reactive UI**: Built with Svelte 5 for smooth, reactive user experience 12 + 13 + ## Running the Example 14 + 15 + 1. **Start the Go Server**: 16 + ```bash 17 + go run main.go 18 + ``` 19 + 20 + 2. In a new terminal, start the Svelte development server: 21 + ```bash 22 + cd static 23 + npm install 24 + npm run dev 25 + ``` 26 + 27 + 3. Open your browser to `http://localhost:3000` 28 + 29 + ## The Problem 30 + 31 + Traditional RPC requires multiple round trips for dependent operations: 32 + 33 + ```javascript 34 + // Sequential: 3 separate HTTP requests (3 round trips) 35 + const user = await api.authenticate('cookie-123'); // Round trip 1 36 + const profile = await api.getUserProfile(user.id); // Round trip 2 37 + const notifications = await api.getNotifications(user.id); // Round trip 3 38 + ``` 39 + 40 + With typical network latency (120ms RTT), this takes ~360ms just for network overhead! 41 + 42 + ## The Solution: Pipeline References 43 + 44 + Cap'n Web RPC's pipelining allows dependent calls to be batched: 45 + 46 + ```javascript 47 + // Pipelined: All 3 calls in 1 HTTP request (1 round trip) 48 + const api = newHttpBatchRpcSession(RPC_URL); 49 + const user = api.authenticate('cookie-123'); 50 + const profile = api.getUserProfile(user.id); // References user.id before it resolves! 51 + const notifications = api.getNotifications(user.id); // References user.id before it resolves! 52 + 53 + const [u, p, n] = await Promise.all([user, profile, notifications]); 54 + ``` 55 + 56 + All three calls are sent together in a single HTTP request. The server resolves them in dependency order and returns all results at once! 57 + 58 + ## How It Works 59 + 60 + ### Backend (Go) 61 + 62 + The Go server implements three RPC methods: 63 + 64 + 1. **`authenticate(sessionToken)`** - Returns a User object with ID and name 65 + 2. **`getUserProfile(userID)`** - Returns profile data for a user 66 + 3. **`getNotifications(userID)`** - Returns an array of notifications for a user 67 + 68 + Sample data: 69 + - Session tokens: `cookie-123`, `cookie-456` 70 + - Users: `u_1` (Ada Lovelace), `u_2` (Alan Turing) 71 + 72 + ### Frontend (Svelte) 73 + 74 + **Pipelined Execution** (1 HTTP request): 75 + ```javascript 76 + const api = newHttpBatchRpcSession(RPC_URL); 77 + const user = api.authenticate('cookie-123'); 78 + const profile = api.getUserProfile(user.id); 79 + const notifications = api.getNotifications(user.id); 80 + 81 + const [u, p, n] = await Promise.all([user, profile, notifications]); 82 + ``` 83 + 84 + **Sequential Execution** (3 HTTP requests): 85 + ```javascript 86 + const api1 = newHttpBatchRpcSession(RPC_URL); 87 + const u = await api1.authenticate('cookie-123'); 88 + 89 + const api2 = newHttpBatchRpcSession(RPC_URL); 90 + const p = await api2.getUserProfile(u.id); 91 + 92 + const api3 = newHttpBatchRpcSession(RPC_URL); 93 + const n = await api3.getNotifications(u.id); 94 + ``` 95 + 96 + ## Performance Benefits 97 + 98 + With default simulated latency (120ms RTT ± 40ms jitter): 99 + 100 + - **Pipelined**: ~1 round trip = ~120-160ms 101 + - **Sequential**: ~3 round trips = ~360-480ms 102 + 103 + **Pipelined execution is typically 60-70% faster!** 104 + 105 + The benefits increase with: 106 + - Higher network latency 107 + - More dependent operations 108 + - Geographic distance between client and server 109 + 110 + ## Testing with curl 111 + 112 + Test the backend directly: 113 + 114 + ```bash 115 + # Authenticate 116 + curl -X POST http://localhost:8000/rpc \ 117 + -d '["push",["pipeline",1,["authenticate"],["cookie-123"]]]' 118 + 119 + curl -X POST http://localhost:8000/rpc \ 120 + -d '["pull",1]' 121 + 122 + # Get profile (using user ID u_1) 123 + curl -X POST http://localhost:8000/rpc \ 124 + -d '["push",["pipeline",2,["getUserProfile"],["u_1"]]]' 125 + 126 + curl -X POST http://localhost:8000/rpc \ 127 + -d '["pull",2]' 128 + 129 + # Get notifications 130 + curl -X POST http://localhost:8000/rpc \ 131 + -d '["push",["pipeline",3,["getNotifications"],["u_1"]]]' 132 + 133 + curl -X POST http://localhost:8000/rpc \ 134 + -d '["pull",3]' 135 + ```
+13 -21
examples/bluesky/README.md
··· 17 18 ## Running the Example 19 20 - ### Start the Go Backend 21 - 22 - ```bash 23 - cd examples/bluesky 24 - go mod tidy 25 - go run main.go 26 - ``` 27 - 28 - The server will start on `http://localhost:8000` 29 - 30 - ### Start the Frontend 31 - 32 - In a new terminal: 33 34 - ```bash 35 - cd examples/bluesky/static 36 - npm install 37 - npm run dev 38 - ``` 39 40 - The Svelte dev server will start on `http://localhost:3000` 41 42 ## How It Works 43 ··· 85 86 - `bsky.app` - Official Bluesky account 87 - `jay.bsky.team` - Jay Graber (Bluesky CEO) 88 - - `pfrazee.com` - Paul Frazee (Bluesky engineer) 89 90 ## Testing with curl 91 ··· 119 120 ``` 121 Browser (Svelte) 122 - ↓ Cap'n Web RPC (batched) 123 Go Server (localhost:8000) 124 ↓ HTTPS 125 AT Protocol API (public.api.bsky.app)
··· 17 18 ## Running the Example 19 20 + 1. **Start the Server**: 21 + ```bash 22 + go run main.go 23 + ``` 24 25 + 2. In a new terminal, start the Svelte development server: 26 + ```bash 27 + cd static 28 + npm install 29 + npm run dev 30 + ``` 31 32 + 3. Open your browser to `http://localhost:3000` 33 34 ## How It Works 35 ··· 77 78 - `bsky.app` - Official Bluesky account 79 - `jay.bsky.team` - Jay Graber (Bluesky CEO) 80 + - `pfrazee.com` - Paul Frazee (Bluesky CTO) 81 82 ## Testing with curl 83 ··· 111 112 ``` 113 Browser (Svelte) 114 + ↓ Cap'n Web RPC 115 Go Server (localhost:8000) 116 ↓ HTTPS 117 AT Protocol API (public.api.bsky.app)
+33
examples/helloworld/README.md
···
··· 1 + # Hello World - Cap'n Web RPC Svelte Demo 2 + 3 + This is a Svelte version of the Hello World demo that shows WebSocket RPC communication between a Svelte frontend and a Go server using the Cap'n Web protocol. 4 + 5 + ## Features 6 + 7 + - **Svelte Frontend**: Modern reactive UI built with Svelte 8 + - **WebSocket RPC**: Real-time bidirectional communication 9 + - **Hot Module Replacement**: Fast development with Vite 10 + - **TypeScript Support**: Ready for TypeScript if needed 11 + 12 + ## Setup and Running 13 + 14 + ### Prerequisites 15 + 16 + - Node.js (v16 or higher) 17 + – Golang v1.21 or higher 18 + 19 + ### Running the Example 20 + 21 + 1. **Start the Server**: 22 + ```bash 23 + go run main.go 24 + ``` 25 + 26 + 2. In a new terminal, start the Svelte development server: 27 + ```bash 28 + cd static 29 + npm install 30 + npm run dev 31 + ``` 32 + 33 + 3. Open your browser to `http://localhost:3000`
-86
examples/helloworld/static/README.md
··· 1 - # Hello World - Cap'n Web RPC Svelte Demo 2 - 3 - This is a Svelte version of the Hello World demo that shows WebSocket RPC communication between a Svelte frontend and a Go server using the Cap'n Web protocol. 4 - 5 - ## Features 6 - 7 - - **Svelte Frontend**: Modern reactive UI built with Svelte 8 - - **WebSocket RPC**: Real-time bidirectional communication 9 - - **Hot Module Replacement**: Fast development with Vite 10 - - **TypeScript Support**: Ready for TypeScript if needed 11 - 12 - ## Setup and Running 13 - 14 - ### Prerequisites 15 - 16 - - Node.js (v16 or higher) 17 - - Go server running (see the Go example in `../../helloworld/`) 18 - 19 - ### Installation 20 - 21 - 1. Install dependencies: 22 - ```bash 23 - npm install 24 - ``` 25 - 26 - ### Development 27 - 28 - 1. Start the Go server (from the `../../helloworld/` directory): 29 - ```bash 30 - cd ../../helloworld/ 31 - go run main.go 32 - ``` 33 - 34 - 2. In a new terminal, start the Svelte development server: 35 - ```bash 36 - npm run dev 37 - ``` 38 - 39 - 3. Open your browser to `http://localhost:3000` 40 - 41 - ### Production Build 42 - 43 - To build for production: 44 - 45 - ```bash 46 - npm run build 47 - ``` 48 - 49 - The built files will be in the `dist/` directory. 50 - 51 - To preview the production build: 52 - 53 - ```bash 54 - npm run preview 55 - ``` 56 - 57 - ## What's Different from the Original 58 - 59 - - **Reactive State**: Uses Svelte's reactive variables instead of DOM manipulation 60 - - **Component-based**: All UI logic is contained in a single Svelte component 61 - - **Better UX**: Loading states, disabled buttons during requests, and smoother interactions 62 - - **Modern Build**: Uses Vite for fast builds and hot module replacement 63 - - **Cleaner Code**: More maintainable and readable code structure 64 - 65 - ## File Structure 66 - 67 - ``` 68 - ├── src/ 69 - │ ├── App.svelte # Main Svelte component 70 - │ └── main.js # Entry point 71 - ├── dist/ # Built files (after npm run build) 72 - ├── index.html # HTML template 73 - ├── package.json # Dependencies and scripts 74 - ├── vite.config.js # Vite configuration 75 - └── svelte.config.js # Svelte configuration 76 - ``` 77 - 78 - ## API Usage 79 - 80 - The demo connects to the Go server's WebSocket endpoint at `ws://127.0.0.1:8000/api` and calls the `hello` method with a name parameter. 81 - 82 - Example RPC call: 83 - ```javascript 84 - const result = await api.hello("World"); 85 - // Returns: "Hello, World!" 86 - ```
···
+12 -57
examples/serverpush/README.md
··· 4 5 ## What This Demo Shows 6 7 - - 💻 **System Metrics Monitoring**: CPU, memory, and network I/O metrics updated in real-time 8 - - 🔄 **Subscription-based Data Feeds**: Client subscribes to specific data streams 9 - - 📊 **Dynamic Data Visualization**: Real-time charts and progress bars 10 - - ⚡ **Polling-based Server Push**: Efficient polling mechanism simulating server push 11 12 ## Architecture 13 ··· 22 23 1. **Start the Server**: 24 ```bash 25 - cd examples/serverpush 26 go run main.go 27 - # or build and run: 28 - go build && ./serverpush 29 ``` 30 31 - 2. **Open the Demo**: 32 ``` 33 - http://localhost:8000/static/serverpush/ 34 - ``` 35 36 - 3. **Test the API** (optional): 37 ```bash 38 # Subscribe to system metrics 39 echo '["push",["pipeline",1,["subscribeSystemMetrics"],[]]] 40 ["pull",1]' | curl -X POST http://localhost:8000/api -H "Content-Type: text/plain" --data-binary @- 41 ``` 42 - 43 - ## Available RPC Methods 44 - 45 - ### `subscribeSystemMetrics()` 46 - - **Returns**: `{subscriptionId, message, status, pollInterval}` 47 - - **Description**: Subscribe to real-time system metrics 48 - 49 - ### `pollMetricsUpdates(subscriptionId)` 50 - - **Parameters**: `subscriptionId` (string) 51 - - **Returns**: `{subscriptionId, updates, hasMore, timestamp}` 52 - - **Description**: Poll for buffered system metrics updates 53 - 54 - ### `unsubscribe(subscriptionId)` 55 - - **Parameters**: `subscriptionId` (string) 56 - - **Returns**: `{subscriptionId, message, status}` 57 - - **Description**: Unsubscribe from a data stream 58 - 59 - ## Data Formats 60 - 61 - ### System Metrics Update 62 - ```json 63 - { 64 - "cpuUsage": 45.2, 65 - "memoryUsage": 62.8, 66 - "networkIO": 23.4, 67 - "timestamp": 1640995200 68 - } 69 - ``` 70 - 71 - ## Features Demonstrated 72 - 73 - - **WebSocket RPC Connection**: Persistent connection for low-latency communication 74 - - **Subscription Management**: Server tracks client subscriptions with unique IDs 75 - - **Data Buffering**: Efficient buffering prevents data loss during polling gaps 76 - - **Real-time Visualization**: Dynamic charts, tables, and progress indicators 77 - - **Error Handling**: Graceful handling of connection issues and invalid subscriptions 78 - - **Resource Management**: Automatic cleanup of subscriptions and polling intervals 79 - 80 - ## Technical Implementation 81 - 82 - - **Backend**: Go with Echo framework and Gorilla WebSocket 83 - - **Frontend**: Vanilla JavaScript with modern ES6 modules 84 - - **RPC Protocol**: Cap'n Web RPC over WebSocket 85 - - **Data Generation**: Background goroutines with realistic price/metrics simulation 86 - - **Concurrency**: Thread-safe subscription management with sync.RWMutex 87 - 88 This example serves as a foundation for implementing real-time data streaming in applications that need to push updates to clients while working within the constraints of request-response protocols.
··· 4 5 ## What This Demo Shows 6 7 + - **System Metrics Monitoring**: CPU, memory, and network I/O metrics updated in real-time 8 + - **Subscription-based Data Feeds**: Client subscribes to specific data streams 9 + - **Dynamic Data Visualization**: Real-time charts and progress bars 10 + - **Polling-based Server Push**: Efficient polling mechanism simulating server push 11 12 ## Architecture 13 ··· 22 23 1. **Start the Server**: 24 ```bash 25 go run main.go 26 ``` 27 28 + 2. In a new terminal, start the Svelte development server: 29 + ```bash 30 + cd static 31 + npm install 32 + npm run dev 33 ``` 34 + 35 + 3. Open your browser to `http://localhost:3000` 36 37 + 4. **Test the API** (optional): 38 ```bash 39 # Subscribe to system metrics 40 echo '["push",["pipeline",1,["subscribeSystemMetrics"],[]]] 41 ["pull",1]' | curl -X POST http://localhost:8000/api -H "Content-Type: text/plain" --data-binary @- 42 ``` 43 This example serves as a foundation for implementing real-time data streaming in applications that need to push updates to clients while working within the constraints of request-response protocols.