35 |
36 |
37 |
--------------------------------------------------------------------------------
/examples/grpc-web/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "grpc-web-example",
3 | "version": "1.0.0",
4 | "description": "",
5 | "private": true,
6 | "scripts": {
7 | "build": "npm run build:typescript && npm run build:webpack",
8 | "build:typescript": "tsc -p .",
9 | "build:proto": "./compile-proto.sh",
10 | "build:webpack": "webpack ./client.js -c webpack.config.js",
11 | "watch:typescript": "npm run build:typescript -- -w",
12 | "watch:webpack": "npm run build:webpack -- -w",
13 | "prebuild": "npm run build:proto",
14 | "lint": "npm run lint:prettier && npm run lint:eslint",
15 | "lint:prettier": "prettier --check \"**/*.{ts,js,json,svg,md,yml}\"",
16 | "lint:eslint": "eslint . --ext .js,.ts",
17 | "lint:fix": "npm run lint:fix:prettier && npm run lint:eslint -- --fix",
18 | "lint:fix:prettier": "prettier --write '**/*.{ts,tsx,js,json,svg,md,yml}'",
19 | "start:server": "node server",
20 | "start:proxy": "docker run -d -p 8080:8080 -v \"$(pwd)\"/envoy.yaml:/etc/envoy/envoy.yaml:ro envoyproxy/envoy:v1.15.0"
21 | },
22 | "keywords": [],
23 | "author": "",
24 | "license": "ISC",
25 | "devDependencies": {
26 | "@grpc/grpc-js": "^1.6.8",
27 | "@grpc/proto-loader": "^0.7.0",
28 | "@tsconfig/node12": "^1.0.11",
29 | "@types/google-protobuf": "^3.15.6",
30 | "@types/node": "^14.14.14",
31 | "@typescript-eslint/eslint-plugin": "^5.31.0",
32 | "@typescript-eslint/parser": "^5.31.0",
33 | "eslint": "^8.20.0",
34 | "eslint-config-prettier": "^8.5.0",
35 | "eslint-plugin-import": "^2.26.0",
36 | "eslint-plugin-node": "^11.1.0",
37 | "eslint-plugin-prettier": "^4.2.1",
38 | "google-protobuf": "^3.21.0",
39 | "grpc-tools": "^1.11.2",
40 | "grpc-web": "^1.3.1",
41 | "prettier": "^2.7.1",
42 | "prettier-plugin-organize-imports": "^3.0.0",
43 | "protoc-gen-grpc-web": "^1.4.0",
44 | "ts-protoc-gen": "^0.15.0",
45 | "typescript": "^4.7.4",
46 | "webpack": "^5.74.0",
47 | "webpack-cli": "^4.10.0"
48 | },
49 | "eslintConfig": {
50 | "ignorePatterns": [
51 | "**/*.js",
52 | "proto/*.ts",
53 | "generated"
54 | ],
55 | "env": {
56 | "browser": false,
57 | "es6": true,
58 | "node": true
59 | },
60 | "parserOptions": {
61 | "project": "./tsconfig.json",
62 | "ecmaVersion": 2018,
63 | "sourceType": "module"
64 | },
65 | "extends": [
66 | "plugin:@typescript-eslint/recommended",
67 | "prettier/@typescript-eslint",
68 | "plugin:prettier/recommended",
69 | "plugin:import/errors",
70 | "plugin:import/warnings",
71 | "plugin:import/typescript"
72 | ]
73 | },
74 | "prettier": {
75 | "singleQuote": true,
76 | "tabWidth": 2,
77 | "printWidth": 80,
78 | "useTabs": false
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/examples/grpc-web/proto/chat.proto:
--------------------------------------------------------------------------------
1 | syntax = "proto3";
2 |
3 | package chat_package;
4 |
5 | message ServerMessage {
6 | string user = 1;
7 | string text = 2;
8 | }
9 |
10 | message ClientMessage {
11 | string user = 1;
12 | string text = 2;
13 | }
14 |
15 | service Chat {
16 | rpc join(ClientMessage) returns (stream ServerMessage) {}
17 | rpc send(ClientMessage) returns (ServerMessage) {}
18 | }
19 |
--------------------------------------------------------------------------------
/examples/grpc-web/server.ts:
--------------------------------------------------------------------------------
1 | import * as grpc from '@grpc/grpc-js';
2 | import { ChatService, IChatServer } from './proto/chat_grpc_pb';
3 | import { ClientMessage, ServerMessage } from './proto/chat_pb';
4 |
5 | const users: grpc.ServerWritableStream