Skip to content

Commit 4af30f2

Browse files
authored
docs(subscriber): add a grpc-web app example (#526)
## Description This pull request adds a real web example for the `grpc-web` feature. It shows how to connect the `console-subscriber` server with a grpc-web client in the browser. I also added a GitHub Actions to make sure we keep updating this example in the future. ## Explanation of Changes 1. It uses vite and react as the basic framework. 2. It uses `connect-es` as the grpc-web client to connect the server. 3. It will spawn an async task to watch the update stream and log the update in the browser's console.
1 parent 4150253 commit 4af30f2

30 files changed

+6972
-1
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
* @tokio-rs/console
1+
* @tokio-rs/console
2+
/console-subscriber/examples/grpc_web @hi-rustin

.github/workflows/ci.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,34 @@ jobs:
101101

102102
- name: Run cargo clippy
103103
run: cargo clippy -- -D warnings
104+
105+
grpc_web:
106+
name: gRPC-web Example
107+
runs-on: ubuntu-latest
108+
defaults:
109+
run:
110+
working-directory: console-subscriber/examples/grpc_web/app
111+
steps:
112+
- name: Checkout sources
113+
uses: actions/checkout@v4
114+
115+
- name: Use Node.js
116+
uses: actions/setup-node@v4
117+
118+
- name: Install dependencies
119+
run: npm install
120+
121+
- name: Lint
122+
run: npm run lint
123+
124+
- name: Format
125+
run: npm run fmt
126+
127+
- name: Generate
128+
run: npm run gen
129+
130+
- name: Check no changes
131+
run: git diff --exit-code
132+
133+
- name: Build
134+
run: npm run build
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# gRPC-web Example
2+
3+
This app provides an example of using the gRPC-web library to facilitate communication between a web browser and a gRPC server.
4+
5+
## Prerequisites
6+
7+
Ensure you have the following installed on your system:
8+
9+
- [Node.js](https://nodejs.org/en/download/) (version 20.10.0 or higher)
10+
- [npm](https://www.npmjs.com/get-npm) (version 10.2.3 or higher)
11+
12+
## Getting Started
13+
14+
Follow these steps to get the application up and running:
15+
16+
1. **Install Dependencies:** Navigate to the `console-subscriber/examples/grpc_web/app` directory and install all necessary dependencies:
17+
18+
```sh
19+
npm install
20+
```
21+
22+
2. **Start the gRPC-web Server:** In the console-subscriber directory, start the server:
23+
24+
```sh
25+
cargo run --example grpc_web --features grpc-web
26+
```
27+
28+
3. **Start the Web Application:** In the `console-subscriber/examples/grpc_web/app` directory, start the web application:
29+
30+
```sh
31+
npm run dev
32+
```
33+
34+
4. **View the Application:** Open a web browser and navigate to `http://localhost:5173`. You can view the output in the developer console.
35+
36+
## Understanding the Code
37+
38+
This example leverages the [connect-es] library to enable communication with the gRPC server from a web browser. The client code can be found in the `console-subscriber/examples/grpc_web/app/src/app.tsx` file.
39+
40+
The [buf] tool is used to generate the gRPC code. You can generate the code using the following command:
41+
42+
```sh
43+
npm run gen
44+
```
45+
46+
For more information about the connect-es library, refer to the [connect-es documentation].
47+
48+
[connect-es]: https://github.com/connectrpc/connect-es
49+
[buf]: https://buf.build/
50+
[connect-es documentation]: https://connectrpc.com/docs/web/getting-started
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
root: true,
3+
env: { browser: true, es2020: true },
4+
extends: [
5+
"eslint:recommended",
6+
"plugin:@typescript-eslint/recommended",
7+
"plugin:react-hooks/recommended",
8+
],
9+
ignorePatterns: ["dist", ".eslintrc.cjs"],
10+
parser: "@typescript-eslint/parser",
11+
plugins: ["react-refresh"],
12+
rules: {
13+
"react-refresh/only-export-components": [
14+
"warn",
15+
{ allowConstantExport: true },
16+
],
17+
},
18+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/gen/**/*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../README.md
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: v1
2+
plugins:
3+
- plugin: es
4+
opt: target=ts
5+
out: src/gen
6+
- plugin: connect-es
7+
opt: target=ts
8+
out: src/gen
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>gRPC-Web Example</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script type="module" src="/src/main.tsx"></script>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)