Skip to content

Commit

Permalink
sample(34-grpc-client-server): Write README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkiu committed Mar 18, 2024
1 parent 938e246 commit 989ba09
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 16 deletions.
81 changes: 81 additions & 0 deletions sample/34-grpc-client-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# gRPC - Client / Server

[nest/sample/04-grpc/](https://github.com/nestjs/nest/tree/master/sample/04-grpc) is a hybrid application (HTTP + gRPC), where the gRPC client and gRPC server are written together as one program.

nest/sample/34-grpc-client-server/ uses the [ts-proto](https://www.npmjs.com/package/ts-proto) package to convert proto files to TypeScript(NestJS).<br>
(ex, `protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=nestJs=true:. ./hero.proto`)<br>
Additionally, multiple proto [sample programs](https://github.com/grpc/grpc-node/tree/master/examples) written as separate clients/servers demonstrate compatibility with the sample programs in `@grpc/grpc-js`.

## Execution

The program source code already contains interface files generated from a `.proto` file, but if you create a new proto file yourself, you must do so via `npm run buill:proto:ts`.

```sh
cd server
npm run buill:proto:ts
npm run start
```

```sh
cd client
npm run buill:proto:ts
npm run start
```

## Compatibility

gRPC Client / gRPC Server that use the same [proto file](https://github.com/grpc/grpc-node/blob/master/examples/protos/helloworld.proto) are compatible with each other.<br>
(All gRPC Server ports are set to 50051.)

| | gRPC Client | gRPC Server |
|-------------------------------------------------------------------------------------------------------------|------------------------------------|------------------------------------|
| [grpc-node/examples/helloworld/](https://github.com/grpc/grpc-node/tree/master/examples/helloworld) | **c-1** `$ node greeter_client.js` | **s-1** `$ node greeter_server.js` |
| [grpc-node/examples/error_handling/](https://github.com/grpc/grpc-node/tree/master/examples/error_handling) | **c-2** `$ node client.js` | **s-2** `$ node server.js` |
| nest/sample/34-grpc-client-server/ | **c-3** `client$ npm start` | **s-3** `server$ npm start` |

- **c-1** --> **s-1**, **s-3**
```shell
$ node greeter_client.js
Greeting: Hello world
$ node greeter_client.js grpc
Greeting: Hello grpc
```
- **c-2** --> **s-2**, **s-3**
```shell
$ node client.js
[1] Calling SayHello with name:""
[1] Received error 3 INVALID_ARGUMENT: request missing required field: name
[2] Calling SayHello with name:"<username>"
[2] Received response Hello <username>
[3] Calling SayHelloStreamReply with name:""
[3] Received expected error 3 INVALID_ARGUMENT: request missing required field: name
[3] Received status with code=INVALID_ARGUMENT details=request missing required field: name
[4] Calling SayHelloStreamReply with name:"<username>"
[4] Received response Hello <username>
[4] Received response Hello <username>
[4] Received response Hello <username>
[4] Received response Hello <username>
[4] Received response Hello <username>
[4] Received status with code=OK details=OK
```
- **c-3** --> **s-1**
```shell
$ curl http://localhost:3000/helloworld/unary/test1
Hello test1
```
- **c-3** --> **s-2**, **s-3**
```shell
$ curl http://localhost:3000/helloworld/unary/test2
Hello test2
$ curl http://localhost:3000/helloworld/streaming/test
["Hello test","Hello test","Hello test","Hello test","Hello test"]
```
- **c-3** --> **s-3**
```shell
$ curl http://localhost:3000/hero/1
{"id":1,"name":"John"}
$ curl http://localhost:3000/hero/2
{"id":2,"name":"Doe"}
$ curl http://localhost:3000/hero
[{"id":1,"name":"John"},{"id":2,"name":"Doe"}]
```
33 changes: 26 additions & 7 deletions sample/34-grpc-client-server/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample/34-grpc-client-server/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@
"ts-loader": "9.5.1",
"ts-node": "10.9.1",
"tsconfig-paths": "4.2.0",
"typescript": "5.3.3"
"typescript": "5.1.6"
}
}
33 changes: 26 additions & 7 deletions sample/34-grpc-client-server/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample/34-grpc-client-server/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@
"ts-loader": "9.5.1",
"ts-node": "10.9.1",
"tsconfig-paths": "4.2.0",
"typescript": "5.3.3"
"typescript": "5.1.6"
}
}

0 comments on commit 989ba09

Please sign in to comment.