diff --git a/sample/34-grpc-client-server/README.md b/sample/34-grpc-client-server/README.md new file mode 100644 index 00000000000..8196ea34484 --- /dev/null +++ b/sample/34-grpc-client-server/README.md @@ -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).
+(ex, `protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=nestJs=true:. ./hero.proto`)
+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.
+(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:"" + [2] Received response Hello + [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:"" + [4] Received response Hello + [4] Received response Hello + [4] Received response Hello + [4] Received response Hello + [4] Received response Hello + [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"}] + ``` diff --git a/sample/34-grpc-client-server/client/package-lock.json b/sample/34-grpc-client-server/client/package-lock.json index 5036dec60ed..593303023b3 100644 --- a/sample/34-grpc-client-server/client/package-lock.json +++ b/sample/34-grpc-client-server/client/package-lock.json @@ -41,7 +41,7 @@ "ts-loader": "9.5.1", "ts-node": "10.9.1", "tsconfig-paths": "4.2.0", - "typescript": "5.3.3" + "typescript": "5.1.6" } }, "node_modules/@ampproject/remapping": { @@ -1855,6 +1855,19 @@ "node": ">=8" } }, + "node_modules/@nestjs/cli/node_modules/typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/@nestjs/common": { "version": "10.3.2", "resolved": "https://registry.npmjs.org/@nestjs/common/-/common-10.3.2.tgz", @@ -9206,9 +9219,9 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" }, "node_modules/typescript": { - "version": "5.3.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", - "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", + "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -10978,6 +10991,12 @@ "dev": true } } + }, + "typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "dev": true } } }, @@ -16422,9 +16441,9 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" }, "typescript": { - "version": "5.3.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", - "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", + "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", "dev": true }, "uid": { diff --git a/sample/34-grpc-client-server/client/package.json b/sample/34-grpc-client-server/client/package.json index 3f6018d68ee..137225dc47b 100644 --- a/sample/34-grpc-client-server/client/package.json +++ b/sample/34-grpc-client-server/client/package.json @@ -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" } } diff --git a/sample/34-grpc-client-server/server/package-lock.json b/sample/34-grpc-client-server/server/package-lock.json index 5036dec60ed..593303023b3 100644 --- a/sample/34-grpc-client-server/server/package-lock.json +++ b/sample/34-grpc-client-server/server/package-lock.json @@ -41,7 +41,7 @@ "ts-loader": "9.5.1", "ts-node": "10.9.1", "tsconfig-paths": "4.2.0", - "typescript": "5.3.3" + "typescript": "5.1.6" } }, "node_modules/@ampproject/remapping": { @@ -1855,6 +1855,19 @@ "node": ">=8" } }, + "node_modules/@nestjs/cli/node_modules/typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/@nestjs/common": { "version": "10.3.2", "resolved": "https://registry.npmjs.org/@nestjs/common/-/common-10.3.2.tgz", @@ -9206,9 +9219,9 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" }, "node_modules/typescript": { - "version": "5.3.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", - "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", + "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -10978,6 +10991,12 @@ "dev": true } } + }, + "typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "dev": true } } }, @@ -16422,9 +16441,9 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" }, "typescript": { - "version": "5.3.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", - "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", + "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", "dev": true }, "uid": { diff --git a/sample/34-grpc-client-server/server/package.json b/sample/34-grpc-client-server/server/package.json index 3f6018d68ee..137225dc47b 100644 --- a/sample/34-grpc-client-server/server/package.json +++ b/sample/34-grpc-client-server/server/package.json @@ -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" } }