/x-nestjs-grpc

eXample Nest.js + gRPC

Primary LanguageJavaScript

x-nestjs-grpc

Environment

$ node -v
v10.17.0

$ yarn -v
1.21.1

Probably the latest version of node.js will cause build errors on node-gyp and grpc.

Setup

$ yarn install

Run

$ yarn start

Connectivity Test

Using gRPCurl

$ go get github.com/fullstorydev/grpcurl
$ go install github.com/fullstorydev/grpcurl/cmd/grpcurl

rpc.RPC/GetChampion

$ grpcurl -d '{"champion_id": 1}' -plaintext -proto rpc/rpc.proto -import-path protos 127.0.0.1:5000 rpc.Rpc/GetChampion
{
  "champion": {
    "championId": 1,
    "type": "ASSASSIN",
    "name": "Akali",
    "message": "If you look dangerous, you better be dangerous."
  }
}

rpc.RPC/ListChampions

$ grpcurl -plaintext -proto rpc/rpc.proto -import-path protos 127.0.0.1:5000 rpc.Rpc/ListChampions
{
  "champions": [
    {
      "championId": 1,
      "type": "ASSASSIN",
      "name": "Akali",
      "message": "If you look dangerous, you better be dangerous."
    },
    {
      "championId": 2,
      "type": "MAGE",
      "name": "Kennen",
      "message": "The Heart of the Tempest beats eternal...and those beaten remember eternally."
    },
    {
      "championId": 3,
      "type": "FIGHTER",
      "name": "Tryndamere",
      "message": "Rage is my weapon."
    }
  ]
}

rpc.RPC/GetBattleField

$ grpcurl -plaintext -proto rpc/rpc.proto -import-path protos 127.0.0.1:5000 rpc.Rpc/GetBattleField
{
  "battleField": {
    "battleFieldId": 2,
    "name": "The Twisted Treeline"
  }
}

Generate code from proto files

This is a bash script that generates code to under codegen dir using proto files under the protos dir.

$ yarn gen

There are two ways to generate code: using grpc-tools and using protbuf.js. The code generated by protobuf.js is kinder and easier to use.